I am just trying to understand what ResultSet being closed even means and its actual definition.
before moving on to these follow up questions
The isClosed() method of the ResultSet interface is used to determine whether the current ResultSet object is closed.
How can I avoid ResultSet is closed exception in Java?
https://www.tutorialspoint.com/how-do-you-check-if-a-resultset-is-closed-or-not-in-jdbc
CodePudding user response:
Closing frees resources
The Javadoc explains:
[Closing] releases this
ResultSet
object's database and JDBC resources
After executing a query:
- The database has resources tied up in maintaining the results. These include using memory, locks on tables and/or rows perhaps, etc.
- Your Java app too has resources tied up in maintaining the results. These include memory, network connections, etc.
Closing the ResultSet
frees up the resources on both the server side and the client side.
So make a habit of closing the ResultSet
as soon as possible, when no longer needed. Do not let it linger. Ditto for other JDBC objects such as Statement
, PreparedStatement
, Connection
, RowSet
, etc.
Try-with-resources
Generally best to make a habit of using the try-with-resources syntax to automatically close these objects. This works on all the JDBC classes marked as AutoCloseable
.
This has been covered many times already, so search Stack Overflow to learn more. You will see full example code (some authored by me).
Be sure to study The Java™ Tutorials by Oracle. Specifically, Trail: JDBC Database Access.
Related Questions:
Try / Try-with-resources and Connection, Statement and ResultSet closing
How should I use try-with-resources with JDBC?
Is it a good practice to put ResultSet into a nested try-with-resources statement after Java7?
try with resources - parameterized PreparedStatements
Using "try with resources" for resources created without any reference