在Spring的JdbcTemplate中对数据库的操作进行了封装,具体的执行代码放在了这个类的execute()方法中。
首先,在JdbcTemplate.java中,对execute()方法进行了重载,有着好几个execute方法。
在下面这段代码中,我觉得值得学习的一个地方就是他的catch()部分。
一般的,只要在finally块中加入了数据库以及statement关闭代码即可,但是在这里,作者在catch块中也加入了数据库关闭代码,其理由就是,防止潜在的数据库连接池死锁。
之前做的项目,用数据库连接池发现无法释放连接,会不会就是这个原因呢? Maybe~
PS:这部分注释是源代码中自带的。
另外,从关闭Statement与Connection的方法名字就可以看出,他们的关闭原理其实是不一样的。Statement确实是关闭,而Connection是返回到数据库连接池之中。
//-------------------------------------------------------------------------
// Methods dealing with static SQL (java.sql.Statement)
//-------------------------------------------------------------------------
public Object execute(StatementCallback action) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
try {
Connection conToUse = con;
if (this.nativeJdbcExtractor != null &&
this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) {
conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
}
stmt = conToUse.createStatement();
applyStatementSettings(stmt);
Statement stmtToUse = stmt;
if (this.nativeJdbcExtractor != null) {
stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt);
}
Object result = action.doInStatement(stmtToUse);
handleWarnings(stmt);
return result;
}
catch (SQLException ex) {
// Release Connection early, to avoid potential connection pool deadlock
// in the case when the exception translator hasn't been initialized yet.
JdbcUtils.closeStatement(stmt);
stmt = null;
DataSourceUtils.releaseConnection(con, getDataSource());
con = null;
throw getExceptionTranslator().translate("StatementCallback", getSql(action), ex);
}
finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}