It's not a secret anymore!
The moose likes JDBC and Relational Databases and the fly likes Tomcat Connection Pool 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 "Tomcat Connection Pool " Watch "Tomcat Connection Pool " New topic

Tomcat Connection Pool

Fernando Galdino

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.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63866

"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.

Forum Bartender

[Asking smart questions] [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.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17421

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.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: Tomcat Connection Pool
It's not a secret anymore!