Question:
In Java, if I close a Statement, does it auto-close all ResultSets?
2006-09-08 10:13:25 UTC
that have been created with that Statement? If I close a Connection, does it close all Statements, PreparedStatements and CallableStatements and ResultSets that have been created using these Statements? Do I have to close each one explicitly or if I close the topmost hierarchy it closes all subordinate / tree level ?
Four answers:
sonfarX
2006-09-08 11:59:50 UTC
In many cases, it is desirable to immediately release ResultSet and Statement's database and JDBC resources instead of waiting for this to happen when it is automatically closed.

So, for a good code, you must close all ResultSet, Statement first, and then the Connection. Connection opens a connection to a DB, and Statements opens a query to a specific table and finally, ResultSet creates a cursor on such table.



Creating:

1.Connection

2.Statement or PreparedStatement or CallableStatement

3.ResultSet



Closing:

4.ResultSet

5.Statement or PreparedStatement or CallableStatement

6.Connection
vincentgl
2006-09-11 07:29:15 UTC
While most JDBC drivers will close and release all related resources when you close the connection, there is no guarantee. A poorly written driver may not properly release associated resources. Those resources will eventually be released as garbage collection takes place, but there could be a significant delay.



Also, if the connections are pooled, then calling close() does NOT close the connection, but instead returns it to the pool.



Therefore, you should ALWAYS explicitly close all database resources as you finish with them.
patel.nachiket
2006-09-08 17:18:47 UTC
see..if you close a statement then all the resultset will not be useful..it will give you error...

so it doesnt mean that all are closed..



and if you close connection then they are not going to be garbage collected properly...(Sorry 4 English speaking problem)
myspace.com/random129
2006-09-08 17:18:40 UTC
Only if there directly linked to it!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...