wood burning stoves 2.0*
The moose likes Tomcat and the fly likes Connection Pool - Should I explicitly close the connection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Connection Pool - Should I explicitly close the connection?" Watch "Connection Pool - Should I explicitly close the connection?" New topic
Author

Connection Pool - Should I explicitly close the connection?

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
If I get a connection from a Connection Pool(the one provided by Tomcat) should I explicitly close the connection? Or should I make the reference point to null or something? Thanks!


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9963
    
163

You should close the connection once you are done with its purpose.


[My Blog] [JavaRanch Journal]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Yes, you should explicitly close the connection.
Not doing so, is the most common cause of memory leaks in Java webapps.

The connection pool in Tomcat will give you a connection object with a close method that has been overrided. Calling it will return the connection to the pool, making it available for new requests.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9963
    
163

Unless you invoke connection.close(), the connection will NOT be returned back to the pool, resulting in a connection leak
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hmmmm... Is closing the Connection per method call a bad thing? Thank you for your answers!
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9963
    
163

Connections should be closed as soon as you are done with it and no longer require it as part of the flow. Does not matter if it is per method. However if another method *requires* the same connection that was opened in some other method then you will have to keep it open. But once the second method is done with its functionality, you can close the connection. Briefly, do not keep connections open if those are not used.
[ September 26, 2006: Message edited by: Jaikiran Pai ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Connection Pool - Should I explicitly close the connection?
 
Similar Threads
Close connection
Singleton class for Database Connection
Connection object in context listenter
Tomcat Connection Pool
is it good to carry the jdbc "Connection" over servlet and Actions ?