简体   繁体   中英

automatic closing of result-set in Java SQL

Consider the following code

ResultSet rs = null;
Statement st = null;
try {
  //do somehting
 } catch (Exception e){
  //do something
 } finally {
     if(st != null){
       try {
       st.close();
       } catch (SQLException e) {
            log.error("Exception while closing statement: " + e);
       }
    }
 }

The question is that when we close the statement, will it close the result set as well or do we need to explicitly close the result set like this

if(rs != null){
   try {
   rs.close();
   } catch (SQLException e) {
        log.error("Exception while closing result set: " + e);
   }
}

I thought that closing the statement will automatically close the result set, but FindBugs throws the following warning if I don't explicitly close the result set

This method may fail to clean up java.sql.ResultSet

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

This and this shows that Oracle may have problems and you might have to explicitly close the ResultSet . But again, as per the Javadocs, this shouldn't be an issue. Hence, the warning,maybe.

You can't count on the ResultSet being closed automatically, it depends on the driver implementation and how compliant it is. The best policy is to close the ResultSet explicitly.

当您关闭语句或连接时,默认情况下也应该关闭它的所有子项。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM