|
From: Trevor C. <pr...@se...> - 2003-09-22 01:38:53
|
I'm currently rewriting some of the tests for the jdbc package, and I have
encountered a potential problem. Specifically, in the JdbcTemplate
"doWithResultSetFromPreparedQuery" method, exception handling does not
appear to always work. Recently changes were made which placed various
"close" calls in a catch (SQLException) block. However, if a Spring
exception is thrown (I noticed this by running a SqlFunction which returned
more than 1 row, thus throwing an "InvalidDataAccessApiUsageException") the
"SQLException" catch is NOT called, and the method exits without closing
resultset/preparedstatements, etc. Note that while I noticed this in the
single method, this behaviour permeates the JdbcTemplate.
The current ("paraphrased") code is:
<code>
try {
... various code ...
SQLWarning warning = ps.getWarnings();
rs.close();
ps.close();
throwExceptionOnWarningIfNotIgnoringWarnings(warning);
} catch (SQLException ex) {
if (rs != null) {
try {
rs.close();
} catch (SQLException ignore) {}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException ignore) {}
}
throw getExceptionTranslator().translate("JdbcTemplate.query(psc) with
PreparedStatementCreator [" + psc + "]", null, ex);
} finally {
DataSourceUtils.closeConnectionIfNecessary(con, this.dataSource);
}
</code>
I propose changing it to:
<code>
try {
... various code ...
SQLWarning warning = ps.getWarnings();
throwExceptionOnWarningIfNotIgnoringWarnings(warning);
} catch (SQLException ex) {
throw getExceptionTranslator().translate("JdbcTemplate.query(psc) with
PreparedStatementCreator [" + psc + "]", null, ex);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ignore) {}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException ignore) {}
}
DataSourceUtils.closeConnectionIfNecessary(con, this.dataSource);
}
</code>
I believe this will ensure resources are closed regardless of
success/failure, and still allow Spring exceptions and "translated"
SQLExceptions to propogate as designed. If anyone sees any holes in this
(specifically Rod/Thomas who I believe have done the most work on the jdbc
packages) please let me know. Otherwise (and barring exceptions) I will
make the changes and commit them with the updated tests tomorrow.
Trevor D. Cook
|