1
votes

Hi I have this problem only in DB2, I am using plain jdbc to execute a simple query.
Then I fetch the ResultSet and, during the fetch of the result set, I call another method which in turn executes another query (opening and closing anoher ResultSet). When the control comes back to the caller my original ResultSet is closed, and this is really weird..
PS: the code I post below works correctly with Sql Server and even with DB2 express, but in the client environment (DB2 on Z/OS) with driver version 3.64.82 no.

..........
    PreparedStatement  pst=conn.prepareStatement(query);
    ResultSet rs=pst.executeQuery();

    while(rs.next()){
        System.out.println("Id:"+rs.getInt("ID"));
        System.out.println("ULTERIORE SPECIFICA   "+getUlterioreSpecifica(conn,rs.getInt("ID")));
        System.out.println("Desc: "+rs.getString("DESC"));
        System.out.println("ETA: "+rs.getInt("ETA"));

    }
    ........

//other method

    private static String getUlterioreSpecifica(Connection conn, int int1) throws Exception{

    String query="select ult_desc from specifica where id=?";
    String retVal="";
      PreparedStatement pst=conn.prepareStatement(query);
      pst.setInt(1, int1);
      ResultSet rs=pst.executeQuery();
      while(rs.next()){
          retVal=rs.getString(1);
      }
      rs.close();
      pst.close();
    return retVal;
}
1
The sql-server tag is for Microsoft SQL Server, it makes no sense to tag a DB2 question with it.Mark Rotteveel
Have you disabled autocommit on the connection?Mark Rotteveel
No, I don't understand why I have to disable autocommit if I am only executing a simple query (select). Could you please explain it? Thanks a lotcsciandr
The answer that was just posted by kjaklik explains it. You don't have to disable autocommit, but driver behavior is influenced by it (especially as a lot of drivers close the cursor on commit and JDBC mandates that executing a new statement in auto commit, will commit a previous transaction automatically)Mark Rotteveel

1 Answers

6
votes

You're probably working in autocommit mode, and probably default behaviour for this specific driver is to close result sets on commit.

so based on comments in How can I avoid ResultSet is closed exception in Java?

what you need to do is set ResultSet.HOLD_CURSORS_OVER_COMMIT when you create the statement.