تخطَّ إلى المحتوى

DB connection not closed (ARCH4084)

هذا المحتوى غير متوفر بلغتك بعد.

Key java.performance.db-connection-not-closed · Severity error · Category performance

If a Connection is opened and not closed, it never returns to the pool. Leaked connections quickly drain the pool, and later requests time out waiting for one.

public int count() throws SQLException {
Connection conn = dataSource.getConnection();
ResultSet rs = conn.createStatement().executeQuery("SELECT count(*) FROM t");
rs.next();
return rs.getInt(1);
}
public int count() throws SQLException {
try (Connection conn = dataSource.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT count(*) FROM t")) {
rs.next();
return rs.getInt(1);
}
}

Open the connection, statement, and result set in try-with-resources so they return to the pool.