Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to put ResultSet into a nested try-with-resources statement after Java7?

According to doc of http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#close() ,

When a Statement object is closed, its current ResultSet object, if one exists, is also closed.

But accoring to Must JDBC Resultsets and Statements be closed separately although the Connection is closed afterwards? , it is seems to be a good practice to explicitly close Connection Statement and ResultSet .

If we still need to close ResultSet, we may need to have a nested try-with-resources statement since we may probably set parameter for Statement like this:

try (Connection conn = connectionProvider.getConnection();
     PreparedStatement pstmt = conn.prepareStatement(sql) {//resources of conn and pst

     setPrepareStatementParameter(pstmt, kvs);//need to set parameters, so I have to put ResultSet into another try-with-resources statement

     try (ResultSet res = pstmt.executeQuery()) {
                ..............

     }
}

Question:

Does put the ResultSet into a separate try-with-resources statement worth anything since the doc states that closing Statement will close the ResultSet

like image 865
JaskeyLam Avatar asked Feb 13 '15 06:02

JaskeyLam


People also ask

What is the advantage of using try with resources statement give an example?

The try-with-resources statement ensures that each resource is closed at the end of the statement execution. If we don't close the resources, it may constitute a resource leak and also the program could exhaust the resources available to it. You can pass any object as a resource that implements java.

Can we use finally with try with resources?

You can use catch and finally blocks with try-with-resources statement just like an ordinary try statement.

Does try with resources need catch?

A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.

Does try with resources close connection on exception?

The Java try with resources construct, AKA Java try-with-resources, is an exception handling mechanism that can automatically close resources like a Java InputStream or a JDBC Connection when you are done with them.


1 Answers

Your example covers too limited a range of the interactions between Connections, Statements, and ResultSets. Consider the following:

try (Connection conn = connectionProvider.getConnection();
     PreparedStatement pstmt = conn.prepareStatement(sql);) {

     for (int i = 0; i < kvs.length; i++) {
         setPrepareStatementParameter(pstmt, kvs[i]);

         // do other stuff

         // Place the ResultSet in another try with resources
         // to ensure the previous iteration's ResultSet
         // is closed when the next iteration begins
         try (ResultSet res = pstmt.executeQuery()) {
             ..............

         }
     }
 }

In the above example, the PreparedStatement is parametrized and executed a kvs.length number of times within the for-loop. Imagine a case in which the parametrization process, for any reason, took a significant length of time. Note that closing the PreparedStatement would do us no good since we want to reuse the compiled SQL statement at every iteration of the for-loop. Then surely nesting the ResultSet into its own try-with-resources block---thus ensuring the prior iteration's ResultSet is closed but the PreparedStatement remains open---is a worthwhile effort.

like image 96
imperfectgrist Avatar answered Oct 18 '22 19:10

imperfectgrist