I am reviewing a big pile of existing code, trying to find unclosed connections that would cause the connection pool to run out or throw other erros.
In some places I see the connection is returned to the pool, the ResultSet is closed, but the PreparedStatement is not closed.
in pseudo code it would look like this:
Connection conn = null;
try {
conn = MyJdbcTemplateHolder.getNewConnectionFromPool();
PreparedStatement ps = conn.prepareStatement(sql, ...);
ResultSet rs = st.executeQuery();
// do stuff with results
} catch(Exception e) {
// exception
} finally {
rs.close();
MyJdbcTemplateHolder.returnConnectionToPool(conn);
//***** Here is what's missing: st.close(); *****
}
The question is: can the open statement cause issues because it wasn't explicitly closed? Or is closing the ResultSet and returning the connection enough?
Obviously I am not talking about one open statement - we have a pool of 100 connections and dozens of places in the code where this issue may come up.
- MySQL version is 5.1
- My JDBC jar is mysql-connector-java-5.1.11-bin.jar