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.
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
Joined: Apr 15, 2006
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.
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 - updated with new look and Ping home screen widget
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.
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