Cursor Is Closed Exception In Java – Fixed (Resolved)

In java, when there is a stored procedure call which returns the Cursor as a ResultSet, there is a possibility that, Cursor is closed exception might occur.

Here is the stack trace which shows that.

java.sql.SQLException: Cursor is closed.
at oracle.jdbc.driver.T4CResultSetAccessor.getCursor(T4CResultSetAccessor.java:323)
at oracle.jdbc.driver.ResultSetAccessor.getObject(ResultSetAccessor.java:85)
at oracle.jdbc.driver.OracleCallableStatement.getObject(OracleCallableStatement.java:1401)
at com.ibm.ws.rsadapter.jdbc.WSJdbcCallableStatement.getObject(WSJdbcCallableStatement.java:443)

Solution to over come the “Cursor is closed” exception is.

If you are not using the Stored Procedure, then make sure the ResultSet is not closed.  If the ResultSet is closed, then the underlying cursor is closed as well.  So make sure, you are not closing the ResultSet and then using it again.   Even closing the Statement would close the ResultSet internally.   So just need to be careful while closing the ResultSet.

Make sure the cursor is opened in the Stored Procedure.  There may be an exception the Stored procedure where in the When Others Block they might have missed to Open the Cursor.  Anytime this occurs, there is a huge possibility of not opening the cursor in any of the condition in the Stored Procedure.

Ref Cursor or the Reference Cursor is pretty useful to pull a huge dataset but need to be careful when it is being opened.  It is best advised to open a the cursor at the top of the procedure and reopen with the actual data whenever required (make sure, you close it before reopening it).

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

You have som very interesting posts. I’m glad I found the link. Thanks.

Matt

Leave a comment

(required)

(required)