From JDBC Spec
Closing a Statement object will close and invalidate any instances of ResultSet produced by that Statement object. The resources held by the ResultSet object may not be released until garbage collection runs again, so it is a good practice to explicitly close ResultSet objects when they are no longer needed.
Does calling ResultSet.close() immediately release a ResultSet object?
Does calling Statement.close() immediately release a Statement object?
Calling Statement.close() releases the ResultSet objects created by a Statement object, so why does it not immediately release the ResultSet object?
From the Javadoc of Statement.close()
(emphasis mine):
Releases this
Statement
object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. It is generally good practice to release resources as soon as you are finished with them to avoid tying up database resources.Calling the method close on a
Statement
object that is already closed has no effect.Note: When a
Statement
object is closed, its currentResultSet
object, if one exists, is also closed.
Here, the Javadoc states that closing a Statement
immediately releases its resources and also closes its current ResultSet
.
And the Javadoc of ResultSet.close()
(emphasis mine):
Releases this
ResultSet
object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.The closing of a
ResultSet
object does not close theBlob
,Clob
orNClob
objects created by theResultSet
.Blob
,Clob
orNClob
objects remain valid for at least the duration of the transaction in which they are created, unless their free method is invoked.When a
ResultSet
is closed, anyResultSetMetaData
instances that were created by calling thegetMetaData
method remain accessible.Note: A
ResultSet
object is automatically closed by theStatement
object that generated it when thatStatement
object is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results.Calling the method close on a
ResultSet
object that is already closed is a no-op.
This Javadoc states that closing a ResultSet
will release its resources immediately as well. And, as stated above, calling Statement.close()
will also close the ResultSet
.
The note also states that only one ResultSet
can be open per Statement
at any given time. The previous ResultSet
will be closed automatically if you make another request (or if any of the other conditions, as stated, are met).
Note: All Javadoc quotes and links are for Java 10.