This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes Tomcat Connection Pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Tomcat Connection Pool " Watch "Tomcat Connection Pool " New topic
Author

Tomcat Connection Pool

Fernando Galdino
Greenhorn

Joined: Jan 08, 2004
Posts: 17
I have a little doubt about Tomcat Connection Pool using jndi/dbcp:

When I do something like it:


why I always need to close the current Connection?
I was think that if I do it the connection will be closed, but I realize that it returns to the pool, right?
How does it function? I call a method to close a connection and instead close it, it return to the pool.

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61458
    
  67

"Fernando GM",

We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi Fernando,

Well, polymorphism (dynamic method invocation) could be your answer. When you call ds.getConnection() you get an object that implements the Connection interface. What actually happens within the close() method of that implementation is up to the concrete implementation. I've not used DBCP or Tomcat connection pooling. Are you sure that what you're saying is correct?

Personally I would prefer to see a PooledConnection.release() or a DBConnectionPool.returnConnection(con) method, which would help to prevent such confusion as you're experiencing.

Hope that helps.

Jules
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16160
    
  21

The close() method on a Connection retrieved from a pool releases that connection back to the pool. A close() on a non-pool connection does whatever the driver implementors felt necessary. Regardless, it's always wise to formally terminate communications, and not only on Connections. Statements should be closed as well. Normally I don't explicitly close ResultSets, since they will (per documentation) close their resultsets, but for a long-term holding, it's handy to know I can.

The best place to close Statements and Connections is in a "finally" clause. Otherwise thrown exceptions may cause resources to leak.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
wood burning stoves
 
subject: Tomcat Connection Pool