views:

41

answers:

3

Hi , I just wanted to know the concept of database pooling.How it is achieved.

+3  A: 

Database connection pooling is a method used to keep database connection open so they can be reused by others.

Typically, opening a database connection is an expensive operation, so pooling keeps the connections active so that, when a connection is later requested, one of the active ones is used in preference to opening another one.

In it's simplest form, it's just a similar API call to the real open-connection API which first checks the pool for a suitable connection. If one is available, that's given to the client, otherwise a new one is created.

Similarly, there's a close API call which doesn't actually call the real close-connection, rather it puts the connection into the pool for later use.

That's a pretty simplistic explanation. Real implementations may be able to handle connections to multiple servers, may pre-allocate some baseline of connections so some are ready immediately, may actually close old connections when the usage pattern quietens down, and so on.

So, something like the following:

  +---------+
  |         |
  | Clients |
+---------+ |
|         |-+        +------+          +----------+
| Clients | ===#===> | Open | =======> | RealOpen |
|         |    |     +------+          +----------+
+---------+    |         ^
               |         |
               |     /------\
               |     | Pool |
               |     \------/
               |         ^
               |         |
               |     +-------+         +-----------+
               #===> | Close | ======> | RealClose |
                     +-------+         +-----------+
paxdiablo
Where do you have the nice little graph from?
Adrian Grigore
I made it from scratch (silly me). If you want to see some decent graphics, have a look at zengr's answer.
paxdiablo
+2  A: 

Images speak a thousand words (paxdiablo gave an awesome description):

alt text

Source

zengr
And apparently, _good_ images speak a few hundred pieces of ASCII art as well :-)
paxdiablo
wow, now that was awesome, how did you generate that?
zengr
A: 

You can use the apache commons library for connection pooling implementation transparently : http://commons.apache.org/dbcp/

DBCP is also a supported Hibernate pool : http://www.informit.com/articles/article.aspx?p=353736&seqNum=4

madhurtanwani