java.sql.sqlRecoverableException: Closed statement: next
Asked Answered
L

2

9

My code throws

java.sql.sqlRecoverableException: Sentencia cerrada: next

which, in English, I guess it would be:

java.sql.sqlRecoverableException: Closed statement: next

This is my code:

public TransactionArray() throws SQLException {

  /* Obtenemos la tabla de transacciones. */
  Connection connection;
  connection = ConnectionManager.getConnection(STATISTIC_DATA_BASE);
  Statement stmt = null;
  String query =
          "select * " +
          "from " + "dCellStatistic" + ".F_Transaction";
  ResultSet rs = null;
  try {
     stmt = connection.createStatement();
     rs = stmt.executeQuery(query);
  } catch (SQLException e ) {
     e.printStackTrace();
  } finally {
     if (stmt != null) { stmt.close(); }
  }

  /* Construimos las transacciones a partir de los registros. */
  List<Transaction> transactionList = new ArrayList<Transaction>();
  while (rs.next()) { //THE PROBLEM ARISES IN THIS LINE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     transactionList.add(new Transaction(rs));
  }
  array = transactionList.toArray(new Transaction[transactionList.size()]);

}

Any clues of what I may be doing wrong? I have seen two threads on Code Ranch about similar issues, but none of them seemed to provide a solution for my case.

Lumbago answered 19/2, 2013 at 13:0 Comment(4)
Your English is pretty good but why is the code in Spanish? :)Wollongong
See the answer from @EricGalluzzo below. For future reference, this behavior is documented in the Javadoc - A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results..Dracula
@Wollongong Thanks for the compliment. I'm Spanish and I work in Spain. We usually write the comments in the language of our country.Lumbago
@adarshr, Oracle has different languages versions that throws errors in the chosen language.Nordine
S
21

You are closing the statement prior to retrieving the information from the result set. Move the stmt.close() call after the rs.next() loop (but keep the try/finally).

Scion answered 19/2, 2013 at 13:2 Comment(2)
It works! But why should I keep the "finally" block if it no longer contains any statement?Lumbago
You should put the rs.next() inside the main try block, and then in the finally block, keep the stmt.close().Scion
L
4

Since the fixed code doesn't fit in a comment, I publish it here for future reference.

public TransactionArray() throws SQLException {
  List<Transaction> transactionList = new ArrayList<Transaction>();

  /* Obtenemos la tabla de transacciones. */
  Connection connection;
  connection = ConnectionManager.getConnection(STATISTIC_DATA_BASE);
  Statement stmt = null;
  String query =
          "select * " +
          "from " + "dCellStatistic" + ".F_Transaction";

  /* Construimos las transacciones a partir de los registros. */
  ResultSet rs;
  try {
     stmt = connection.createStatement();
     rs = stmt.executeQuery(query);
     while (rs.next()) {
        transactionList.add(new Transaction(rs));
     }
  } catch (SQLException e ) {
     e.printStackTrace();
  } finally {
     if (stmt != null) {
        stmt.close();
     }
  }
  array = transactionList.toArray(new Transaction[transactionList.size()]);
}
Lumbago answered 19/2, 2013 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.