连接池的close()方法是重写了的,它实际上不会真正的关闭数据库连接,而是将它标志为空闲。但是你一定要调用close()方法,否则别人就无法再使用这个连接了。这也就是我们常说的数据库连接泄漏的一种情形。使用了Spring的JDBCTemplate以后,不需要显式的调用close()方法,是因为我们通过配置让Spring帮我们作了这个工作。这些工作不是不需要了,而是Spring帮我们做了。仅此而已。你看一下Spring的JDBCTemplate的这个方法,就明白了: 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.getWarnings());
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());
}
}
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.getWarnings());
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());
}
}
解决方案 »
- struts2里面的struts-plugin.xml配置文件有什么用啊
- 各位大侠帮帮忙忙,在现等
- 关于C#调用java webservice时传递数据的问题
- 如何解决servlet中的中文显示问题
- 请问如果JMS服务器死了,客户端的信息能够在客户端保存吗?
- 关于SOCKET通信的问题,怎样结束本次发送的字符?
- 知道servlet下通过hiberante访问数据库的朋友请进来一看,一事相求!
- 用struts,采用MVC模式,对数据查询显示的页面中我想加入checkbox来控制删除功能?
- 立即给分:笨鸟提问:请问可以使用struts来完全取代javascript吗?比如对form里select的change控制等???
- SSH+MySQL 主键类型配置
- struts1,标签(bean:write)需要输出的bean是一个子类,标签能取到父类的属性吗?
- 关于resultset结果集中数据类型判断的问题
我们不用关心他内部怎么处理的,我们只需要 con.close()就可以了!
只需要记住下面的
ResultSet.close() ;
PreparedStatement.close() ;
DataBaseConnection.close() ;