相关文章推荐
玩滑板的黄花菜  ·  python ...·  4 月前    · 
天涯  ·  StarUML 在不同文件间 ...·  3 年前    · 
9976254  ·  知乎 - 有问题,上知乎·  4 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I recently upgraded the Oracle JDBC driver version from 11.2.0.1 to 12.1.0.1. However, a very strange thing I noticed. In one of my tests I was closing the ResultSet object twice using following code in Java:

if (resSet != null) {
            try {
                resSet.getStatement().close();
            } catch (NullPointerException ne) {
                // nothing to do!
                // NullPointerException seen at Oracle and Sybase!

This worked without any exception when I used old JDBC driver 11.2.0.1. But when I switch to new JDBC driver 12.1.0.1, it throws an exception:

java.sql.SQLException: Closed Resultset: getStatement
    at oracle.jdbc.driver.InsensitiveScrollableResultSet.getStatement(InsensitiveScrollableResultSet.java:272)

Why does this happen? When I checked the JavaDoc of ResultSet it says: Calling the method close on a ResultSet object that is already closed is a no-op.

So now I am confused, calling a close twice is a no-op means it shouldn't throw any exception, right?

I am using JDK7u45 with java.sql.ResultSet.

I am sorry I didnt mention clearly, I am already using try-catch block. I just need to know why there is a difference in behavior. Updating my question now... – ParagJ Dec 30, 2013 at 12:12 what is this code supposed purpose? As Henry said, it's getStatement() that throw java.sql.SQLException: Closed Resultset not close() – user180100 Dec 30, 2013 at 12:14

As already indicated in the comments, you are not calling ResultSet.close(), but calling ResultSet.getStatement() followed by a Statement.close() instead.

And that call to resSet.getStatement() is what throws the exception, as is required by the JDBC spec/api doc:

Throws:
SQLException - if a database access error occurs or this method is called on a closed result set

(emphasis mine)

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.