Jasmine Baretto
Jasmine Baretto

Reputation: 1

Caught a throwable exception during processing Closed Resultset: next

So I did try checking for answers before I could ask this question again, I'm getting a closed ResultSet exception for my code below. The same code worked on development environment when tested for a small set of records. But on QA environment the exception is encountered for 200-300 records being fetched by the query as well. My question is, if there is no close statement or close connection code why is the closed resultset exception thrown at the While loop in the code below?

public void extractRecordsAndUpdateData() throws Throwable {
    ConnectionManager mgr =null;

    /*
    * Some authentication code here for user authentication to allow access in the application
    */

    Connection c = null;
    try {
        mgr = mServiceLocator.getConnectionManager();
    }
    catch (Exception newex) {
        newex.printStackTrace();
        customLogWriter.logEntry("Got a Exception during authentication " + newex.getMessage());
    }

    PreparedStatement pSql = null;
    ResultSet myResultSet = null;

    try {
        c = mgr.getConnection(mgr.getSiteName());
        pSql = c.prepareStatement(extractSQL); // extractSQL is a simple select statement fetching records from DB to be processed in the while loop below.
        myResultSet = pSql.executeQuery();

    }catch(SQLException ex){customLogWriter.logEntry("Error " + ex.getMessage());}

    List<List> outerList=new ArrayList<List>();
    while (myResultSet.next())  // Exception encountered on this line of code
    { 
        /*Do some processing*/
    }

    customLogWriter.close();
}

Upvotes: 0

Views: 89

Answers (1)

user207421
user207421

Reputation: 310936

Poorly structured exception handling. The ResultSet.next() loop should be inside the try block. You should only have one try and one catch (SQLException ...) here.

Don't write code like this. Code that depends on the success of code in a prior try block should be inside that try block.

Upvotes: 1

Related Questions