Statement.close with Unprocessed Results

The JDBC specification deos not clearly address how a driver should behave when you call Statement.execute and later call close on that same statement object without processing all of the results (update counts and ResultSets) returned by the Statement.

For example, assume that there is a stored procedure on the database that performs seven row inserts. An application then executes that stored procedure using a Statement.execute. In this case, a Sybase database returns seven update counts (one for each inserted row) to the application. In normal JDBC application logic, you would process those update counts in a loop using the getMoreResults, getResultSet and getUpdateCount methods. These are clearly explained on the Java SE documentation in the Javadoc for the java.sql.* package.

However, an application programmer might incorrectly call Statement.close before reading through all of the returned update counts. In this case, jConnect sends a cancel to the database, which might have unexpected and unwanted side effects.

In this particular example, if the application calls Statement.close before the database has completed the inserts, the database might not execute all of the inserts. It might stop, for example, after only five rows are inserted because the cancel is processed on the database before the stored procedure completes. jConnect throws a SQLException when you try to close a Statement when there are still unprocessed results.

The missing inserts would not be reported to you. jConnect programmers are strongly advised to adhere to these guidelines:
  • When you call Statement.close, a cancel is sent to the server if not all the results (update counts and ResultSets) have been completely processed. In cases where you only executed select statements, this is fine. However, in cases where you executed insert/update/delete operations, this might result in not all of those operations completing as expected.

  • Therefore, you should never call close with unprocessed results when you have executed anything but pure select statements.

  • Instead, if you call Statement.execute, be sure your code processes all the results by using the getUpdateCount, getMoreResults, and getResultSet methods.