Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tomcat Connection Pool

 
Fernando Galdino
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18303
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic