Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection Pool - Should I explicitly close the connection?

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should close the connection once you are done with its purpose.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you invoke connection.close(), the connection will NOT be returned back to the pool, resulting in a connection leak
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm... Is closing the Connection per method call a bad thing? Thank you for your answers!
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic