aspose file tools*
The moose likes JDBC and the fly likes difference between connection.close() and con = null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "difference between connection.close() and con = null" Watch "difference between connection.close() and con = null" New topic
Author

difference between connection.close() and con = null

Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
If I explicitly close the connection by calling close() on connection object and when I de reference connection variable by setting it to null, what is the difference?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

One closes the connection, one sets the connection reference to null.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

If you don't close the connection, you can have a connection leak. It is important to close the connection in a finally block.

Moving to our JDBC forum.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Jeanne Boyarsky wrote:If you don't close the connection, you can have a connection leak. It is important to close the connection in a finally block.

Moving to our JDBC forum.


yes you are right, I have stale connection in Oracle database. But close() also nulls this connection reference inside, isn't it? What is the diff between two operations then?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No. The close() operation closes the connection--it doesn't do *anything* to the connection reference. You might not be able to *do* anything with the connection, but it's not null. Once it's closed it can be released back into a collection pool, but that's something different yet again.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
David Newton wrote:No. The close() operation closes the connection--it doesn't do *anything* to the connection reference. You might not be able to *do* anything with the connection, but it's not null. Once it's closed it can be released back into a collection pool, but that's something different yet again.

So, close() operation does something in connection object and releases some resource. Now initially we load driver class and create connection object by calling DriverManager.getConnection(), which creates an object of type Connection. So, when we close() the connection (layman term), then what exactly it does with that connection object or driver class ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Start here. Then try here here and here.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Rahul.p Kumar wrote:when we close() the connection (layman term), then what exactly it does with that connection object or driver class


You don't know and you don't care.

Its not for a programmer to look inside a JDBC driver and determine how/what it does. The only thing you need to know is that any time you get a connection you *must* close it (hence the finally block) else you may leave that connection open in your application and eventually run out of available connections. This isn't c++ where there's an object deconstructor, setting a object to null really doesn't do anything special except possibly lose any reference to the connection you have yet to close.

After a connection is closed, it is still a valid object. You can set it to null if you'd like but there's rarely a reason to since it should go away anyway after the function finishes. Forget about what happens inside of Connection.close() since it can vary among JDBC drivers. As long as it properly closes the active connection with the database (which all stable ones will do), you don't care about the details. That's why its a factory/blackbox pattern.


My Blog: Down Home Country Coding with Scott Selikoff
Malhar Me
Greenhorn

Joined: Jun 22, 2009
Posts: 27
connection.close() - it close the conection with the databse nad release all the resources.
con = null - the reference to connection object is deleted in that case if the connection are open then it still open i.e. resources are not free.
Aneesh Vijendran
Ranch Hand

Joined: Jun 29, 2008
Posts: 125
HI,

The close method does the following:


Close the ConnectionImpl
ConnectionWrapper
PooledConnection
Sets all these reference to null

Calles the javax.sql.ConnectionEventListener and snd teh ClosedEven Notifier

And finally all depends on teh drivers. But any good driver should do all these.

Cheers
Aneesh


Cheers
Aneesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between connection.close() and con = null