File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between connection.close() and con = null

 
Rahul P Kumar
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One closes the connection, one sets the connection reference to null.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33671
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start here. Then try here here and here.
 
Scott Selikoff
author
Saloon Keeper
Posts: 3887
15
Eclipse IDE Flex Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Malhar Me
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic