GeeCON Prague 2014*
The moose likes JDBC and the fly likes regarding connection pooling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "regarding connection pooling" Watch "regarding connection pooling" New topic
Author

regarding connection pooling

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
As we all know the concept of connection pooling.I have a doubt for that.
If connection pooling would not have been done by container,how would we have implemented ,i mean the ways in which we can implement this thing and how.This was asked in an interview.
Muth Rama
Greenhorn

Joined: Nov 21, 2007
Posts: 2
One way I would say, create a n(say 50 or something) number of connection objects and keep it in some queue(I mean some collection) objects. Then whenever the database connection needed by application, get the available connection object from that queue and use it. Once the need over just give back the object to the queue and make it free. So that next time anyone can use that. This is way connection pooling also working.
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
Hi Rama,

Your reply seems good.But,can there be some more efficient way than this.This question is for all but specially for ranchers who have not told anything till now.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42030
    
  64
What do you mean by "more efficient"? Getting an object from a pool has very little overhead. The pool itself generally includes a low-priority background thread that checks connections for liveness, and opens or closes them as needed. That hardly seems worth worrying about.


Ping & DNS - my free Android networking tools app
Andy Grove
Greenhorn

Joined: Nov 11, 2003
Posts: 18
There are many open source connection pool libraries that you can use rather than implementing your own connection pool. Apache DBCP is a popular one. You can use this in your code regardless of whether your code is deployed in a container or not.

Hope that helps.
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
thanks
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
One approach makes clients aware of the pool. Instead of doing the normal get connection and close connection they have to do pool.get() and pool.return(). You can use a very simple object pool made out of a blocking queue. Load it up with connections at startup and let people get and return them all day long. Wrap it up with more code if you need to grow and shrink the pool.

A much more elaborate approach hides the pool behind the normal methods. When you ask the DriverManager for a new connection, you actually get one from the pool. When call close, it actually puts itself back in the pool. You can write one of these, too, but it's a lot of work to get right.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
GeeCON Prague 2014
 
subject: regarding connection pooling