File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Closing a Pooled Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Closing a Pooled Connection" Watch "Closing a Pooled Connection" New topic

Closing a Pooled Connection

Mark Butcher
Ranch Hand

Joined: Jun 28, 2012
Posts: 46
Hi friends,

Internally, what happens when we try to close a pooled connection object manually ??

Precisely, what happens to that object internally, if we get it from a connection pool and
then call connectionObject.close() method on that ??

Martin Vajsar

Joined: Aug 22, 2010
Posts: 3733

When you call close on the Connection instance you got from the pool, it usually means the connection gets returned to the pool and can be reused (it depends on the connection pool implementation, but I'd be surprised to find a connection pool which would do something different).

You might also obtain the physical connection (at least with some pool implementations) and close that one. However, the pool probably won't know what happened and will try to reuse the connection if you return in to the pool afterwards, in which case the next user of that connection will get an error (sometimes the pools verify connections before handing them out). However, there is no good reason to ever do something like this.
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Martin is correct.

A pool manager simply allocates a previously open connection (or opens one) to your app.

When you call "close()", it tags that entry as ready to be reused by another application.

Keeping the connections open is more for performance tuning by not suffering the cost of physical
opening and closing for every transaction (one of more requests).

Mark Butcher
Ranch Hand

Joined: Jun 28, 2012
Posts: 46
That means closing the connection object doesn't close it, but actually returns the object back to the connection pool.
I agree. Here's the link:
subject: Closing a Pooled Connection
It's not a secret anymore!