java – DBCP连接池connection.close()是否返回到池的连接
作者:互联网
如果我们执行getConnection(),则使用DBCP中的BasicDataSource,在finally块中我们关闭连接,它确实会返回到池的连接,或者是否关闭连接.我正在检查的代码片段就是这个
try {
Connection conn1 = getJdbcTemplate().getDataSource()
.getConnection();
//Some code to call stored proc
} catch (SQLException sqlEx) {
throw sqlEx;
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex1) {
throw ex1;
}
}
我正在检查BasicDataSource的源代码,我到达了这个连接的包装类.
private class PoolGuardConnectionWrapper extends DelegatingConnection {
private Connection delegate;
PoolGuardConnectionWrapper(Connection delegate) {
super(delegate);
this.delegate = delegate;
}
public void close() throws SQLException {
if (delegate != null) {
this.delegate.close();
this.delegate = null;
super.setDelegate(null);
}
}
委托对象,如果是java.sql.Connection类型.包装器代码调用委托的close方法,该方法将关闭集合而不是将连接返回到池.这是DBCP的已知问题,还是我错误地阅读了源代码请告诉我.
谢谢
解决方法:
你最终会调用PoolableConnection.close(),它将它返回到池中.
标签:java,connection-pooling,database-connection,apache-commons-dbcp 来源: https://codeday.me/bug/20190609/1206082.html