• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

difference between connection.close() and con = null

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One closes the connection, one sets the connection reference to null.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start here. Then try here here and here.
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic