Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid ResultSet is closed exception in Java?

As soon as my code gets to my while(rs.next()) loop it produces the ResultSet is closed exception. What causes this exception and how can I correct for it?

EDIT: I notice in my code that I am nesting while(rs.next()) loop with another (rs2.next()), both result sets coming from the same DB, is this an issue?

like image 256
Bobby Avatar asked Jun 01 '09 16:06

Bobby


People also ask

Does ResultSet need to be closed?

You should explicitly close Statements , ResultSets , and Connections when you no longer need them, unless you declare them in a try -with-resources statement (available in JDK 7 and after). Connections to Derby are resources external to an application, and the garbage collector will not close them automatically.

What happens if ResultSet is not closed?

In the mean time many result sets might opened and left unclosed. If it happens on a single database by multiple applications, the data updation is not perfect and may violate ACID properties rule for data presuming.

How do I know if my ResultSet is closed?

The ResultSet object contains a cursor/pointer which points to the current row. Initially this cursor is positioned before first row (default position). The isClosed() method of the ResultSet interface is used to determine whether the current ResultSet object is closed.


1 Answers

Sounds like you executed another statement in the same connection before traversing the result set from the first statement. If you're nesting the processing of two result sets from the same database, you're doing something wrong. The combination of those sets should be done on the database side.

like image 106
Apocalisp Avatar answered Oct 05 '22 17:10

Apocalisp