Statement and Resultset close after connection close
NickName:seattledev Ask DateTime:2012-08-01T07:53:03

Statement and Resultset close after connection close

I have recently moved to a project where I am encountering a lot of code of this nature - (This is using the jdbc postgres driver)

try {
    Connection conn = pool.getAConnection(); //home-grown conn pool 
    PreparedStatement ps = ..;
    ResultSet rs = ..;
    rs = ps.executeQuery();
    ...
 } catch (SQLException se) {
    conn.close(); 
} finally {
    if (stmt != null) stmt.close();
    if (rs != null) rs.close();
}

Apparently this code has been in production for a while, without causing issues.

What I find hard to understand is, in the exception flow, the connection gets closed or returned to the pool first; and then the statement and resultset are attempted to be closed. Does it make sense to execute this after the parent connection object is closed ?

Because of the way the code is structured, connection release has to be done in the exception block. That cannot be changed. That being said, is it okay to leave the stmt.close() and rs.close() in finally after the connection has been released to the pool ?

To clarify further, if my understanding is correct (i.e., statement and resultset must be closed before connection close and not after), I need to repeat some code between the catch and finally. The revised code now looks as below. Can this be simplified ?

try {
...
} catch(Exception ex){
      if (rs != null) {
         close(rs); rs = null; // close() method impl just calls rs.close() in try-catch block
      }
      if (ps != null) {
         close(ps); ps = null;
      }
      processException( ex, con); // This method logs and then either closes the connection or releases to pool, depending on some conditions. 
      con = null;
  } finally {
      if (rs != null) {
          close(rs); 
      }
      if (ps != null) {
          close(ps); 
      }             
      if (null != con) {
          close(con);
      }
  }

Just for perspective, this code is all over - at least a 100 or so methods ! I would like to simplify this further if possible. Appreciate your feedback.

Copyright Notice:Content Author:「seattledev」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/11750708/statement-and-resultset-close-after-connection-close

More about “Statement and Resultset close after connection close” related questions

Statement and Resultset close after connection close

I have recently moved to a project where I am encountering a lot of code of this nature - (This is using the jdbc postgres driver) try { Connection conn = pool.getAConnection(); //home-grown ...

Show Detail

Close ResultSet and Statement resources (datastax, no close() method)

Hello World ! I'm in trouble trying to close some datastax resources (Statement, ResultSet). Sonar is yelling at me to close those resources after i use them. (for information after i use this my...

Show Detail

how to close sql resultset, statement, connection correctly

I am wondering, why sonarQube display error msg, that resultSet and Statement are not closed. SonarQube give me the errormsgs: Close this "Statement". Close this "ResultSet". My current code looks

Show Detail

Java - Can't use ResultSet after connection close

I have a problem with closing a connection to MySQL. I'm getting the error: java.sql.SQLException: Operation not allowed after ResultSet closed My code: public static ResultSet sqlquery (Str...

Show Detail

How can I close the ResultSet, Statement and Connection in this code?

How can I close the ResultSet, Statement and Connection in this code because after few clicks, program gives me the error com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data ...

Show Detail

When to close Connection, Statement, PreparedStatement and ResultSet in JDBC

Few questions on JDBC coding: For a single client application, do we need a Connection pool? Is it a good idea to create a Connection at the beginning and keep it alive without close it until

Show Detail

When to close Connection, Statement, PreparedStatement and ResultSet in JDBC

Few questions on JDBC coding: For a single client application, do we need a Connection pool? Is it a good idea to create a Connection at the beginning and keep it alive without close it until

Show Detail

try/catch/finally with connection.close()

I need to return the data from method first and then call the connection,but warning tells me: "This method should return a result of type ResultSet», adding a return to the end of the method, afte...

Show Detail

Is there a method of doing close from the outside of the method of generating Statement and ResultSet generated from Connection?

I am Japanese. English is unskilled. I'm sorry. Is there a method of doing close from the outside of the method of generating Statement and ResultSet generated from Connection? However, I want to

Show Detail

if I forget to close the connection to the database

if i do close to connection database instead if i do close my resultSet ,what happend ? I did write example to under. For example. Database.java public class Database{ try{ Connection con;

Show Detail