Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does closing Connection automatically close statement and resultset? [duplicate]

Tags:

I know the safe pattern in Java is to close your ResultSet, Statement, and Connection in order in a finally block.

If you close connection and then try to close statement(doesnt throw exception). But if you try to call any method from statement an exception is thrown.

I was wondering does closing connection automatically close all the statement objects created out of that connection?

Update:
I am using DatabaseProductVersion: Oracle Database 11g Release 11.1.0.0.0
DriverName: Oracle JDBC driver
DriverVersion: 10.2.0.4.0

like image 237
Srujan Kumar Gulla Avatar asked Dec 24 '12 17:12

Srujan Kumar Gulla


People also ask

Does closing connection close ResultSet?

Closing connection object closes the statement and resultset objects but what actually happens is that the statement and resultset object are still open (isClosed() returns false).

Do we need to close the ResultSet?

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.

Why should you close a JDBC connection after editing?

If you don't close it, it leaks, and ties up server resources. @EJP The connection itself might be thread-safe (required by JDBC), but the applications use of the connection is probably not threadsafe.

What is the correct order to close database resources?

The rules for closing JDBC resources are: The ResultSet object is closed first, then the Statement object, then the Connection object.


2 Answers

Yes it does, Connection.close API says "Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be automatically released". The problem is that applications typically use database connection pools and these may simply return Connection to pool on Connection.close.

In any case, it's a good practice to always close ResultSet and Statement explicitly and not to rely on Connection.close.

Besides, it's not the best idea to work with JDBC directly. You can use Spring JDBC instead and forget about releasing resources problem.

like image 162
Evgeniy Dorofeev Avatar answered Oct 04 '22 12:10

Evgeniy Dorofeev


The details are ultimately down to each JDBC driver implementation; however, once a connection to the database is closed, everything related to it is disposed at the DB side, so there is nothing much the client side can do but auto-close the objects representing these resources.

You never know in what ways the databeses/drivers could be broken (there may be resource leaks, for example), therefore the best practice recommendation is to close everything explicitly.

like image 37
Marko Topolnik Avatar answered Oct 04 '22 13:10

Marko Topolnik