Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Closing a DB connection retrieved from connection pool  RSS feed

 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I configured a connection pool in tomcat 7. For every database activity I get a connection from the pool. Do I have to close the connection like other regular connection after the database operation is done? If I close the connection will it have any effect on the connection pool?
 
Claude Moore
Ranch Hand
Posts: 862
8
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Shahid,

every time you get a connection (or, more in general, a resource) from a connection pool (or, more in general, a resource pool), you absolutely need to release it invoking close() method. This way, used resourced is freed and returned to the pool, so that another thread may use it. A connection pool tipically mantains a fixed number of connections: if you don't return them to the pool, the pool will no have more available connections that application thread may obtain, and such thread will result to hang waiting forever.
 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Claude.
 
Claude Moore
Ranch Hand
Posts: 862
8
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.
 
chandra varma
Greenhorn
Posts: 2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes due to the orphan connections , application going to stop. So you have to carefully close the connections in application.

 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Chandra Varma
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Useful info:

A JDBC Connection Pool doesn't actually return raw Connection objects. What it does is create façade Connection objects and returns them to application requesters. For the most part, these façades pass requests straight through to the underlying actual Connection.

An exception is the façade's close() method. The close() method for an actual JDBC Connection object closes the database connection and severs its link to the database, effectively destroying it.

The façade object's close() method doesn't do that. It simply returns the Connection to the pool so that some other requester can obtain it without all the overhead that it takes to construct and initialize a Connection object from scratch.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!