我们看到我们的数据库连接因org.apache.commons.dbcp.BasicDataSource
套接字写入错误而死的情况:
com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset by peer: socket write error
当然,所有后续写入连接的尝试都会失败:
com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
在更新代码以捕获此类异常并在发生时请求新连接后,它再次失败。我是否正确怀疑DataSource#getConnection()
每次调用时调用实际上并没有提供新的连接?不只是重用已关闭的现有连接吗?
如果我是正确的,那么丢弃旧连接并请求新连接的正确方法是什么?
编辑:这是我想知道的更简洁的版本:
Connection c1, c2;
c1 = DatabaseManager.getConnection();
// c1.close() not called
c2 = DatabaseManager.getConnection();
“c1 == c2”是一个真实的陈述吗?还是分配了两个连接?如果是后者,这样的代码是否代表“连接池泄漏”:
Connection c1;
c1 = DatabaseManager.getConnection();
// c1.close() not called
c1 = DatabaseManager.getConnection();