该异常表示泄漏数据库连接的应用程序代码的典型情况 . 您需要确保在根据普通JDBC习惯用法的同一方法块中的try-with-resources块中获取所有这些( Connection , Statement and ResultSet ) and .
public void create(Entity entity) throws SQLException {
try (
Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(SQL_CREATE);
) {
statement.setSomeObject(1, entity.getSomeProperty());
// ...
statement.executeUpdate();
}
}
或者当你没有使用Java 7时,在 try-finally 块中 . 在 finally 关闭它们将保证在例外情况下它们也会关闭 .
public void create(Entity entity) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = dataSource.getConnection();
statement = connection.prepareStatement(SQL_CREATE);
statement.setSomeObject(1, entity.getSomeProperty());
// ...
statement.executeUpdate();
} finally {
if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
}
}
是的,即使使用连接池,您仍需要自己关闭连接 . 在初学者中,他们认为它会自动处理结束是一个常见的错误 . 这是 not true . 连接池返回一个包装的连接,它在close()中执行类似下面的操作:
public void close() throws SQLException {
if (this.connection is still eligible for reuse) {
do not close this.connection, but just return it to pool for reuse;
} else {
actually invoke this.connection.close();
}
}
不关闭它们会导致连接没有被释放回池中以便重复使用,因此它将一次又一次地获取一个新连接,直到数据库用尽连接,这将导致应用程序崩溃 .
另见: