How connection pooling works in Java and JDBC

By: Ramlak  

Connection pooling has become the standard for middleware database drivers. The process of creating a connection, always an expensive, time-consuming operation, is multiplied in these environments where a large number of users are accessing the database in short, unconnected operations. Creating connections over and over in these environments is simply too expensive.

The transaction profile for Web applications, probably the most common application in use today, is that of a large number of users performing short, discrete database operations. These applications usually perform work centered around creating a web page that will be sent back to the user's browser. Transactions are generally short-lived, and user sessions are often limited in time.

A connection pool operates by performing the work of creating connections ahead of time, In the case of a JDBC connection pool, a pool of Connection objects is created at the time the application server (or some other server) starts. These objects are then managed by a pool manager that disperses connections as they are requested by clients and returns them to the pool when it determines the client is finished with the Connection object. A great deal of housekeeping is involved in managing these connections.

When the connection pool server starts, it creates a predetermined number of Connection objects. A client application would then perform a JNDI lookup to retrieve a reference to a DataSource object that implements the ConnectionPoolDataSource interface. The client application would not need make any special provisions to use the pooled data source; the code would be no different from code written for a nonpooled DataSource.

When the client application requests a connetion from the ConnetionPoolDataSource, the data source implementation would retrieve a physical connection to the client application. the ConnectionPoolDataSource would return a Connection object that implemented the PooledConnection interface.

The PooledConnection interface dictates the use of event listeners. These event listeners allow the connection pool manager to capture important connection events, such as attempts by the client application to close the connection. When the driver traps a close-connection event, it intercedes and performs a pseudo-close operation that merely takes the Connection object, returns it to the pool of available connection, and performs any housekeeping that is necessary.

The operation of the connection pool should be completely transparent to the client application. The triggering of connection events, the manipulation of the object pool, and the creation and destruction of physical connections are all managed by the pool manager. The activities of the connection pool are, however, configurable by the application developer or the application deployer

Archived Comments

1. reallt waste ...without code.. :(
View Tutorial          By: LETS_ROCK at 2015-03-25 07:42:24

2. Very usefull explanation. This was saved in my personal notes about java architecture!

View Tutorial          By: JRicharsz at 2015-02-05 15:51:28

3. 1. Performance

Object pooling provides better application performance As object creat

View Tutorial          By: sonia at 2013-09-21 20:22:49

4. Please add the code snippet ..
View Tutorial          By: mari selvam at 2012-08-23 10:13:51

5. if suppose i am having connection pool max size 30 and 30 connections are active for 1 hr then, what
View Tutorial          By: Shrikant at 2012-07-20 12:09:33

6. thanks its good
can u provide exmaple with sql server


View Tutorial          By: user at 2012-07-14 16:36:21

7. thanks its good
can u provide exmaple with sql server


View Tutorial          By: user at 2012-07-14 16:35:55

8. good explanation .... Thanks
View Tutorial          By: nazeer at 2012-05-24 09:18:19

9. Assume scenario:
- i have set connection pool size as 5 in my application.
- when 5 us

View Tutorial          By: Dan at 2012-02-22 09:48:46

10. If the database server goes down, should the connection pool be refreshed. If conection pool is not
View Tutorial          By: Naveen Gowd at 2012-02-02 07:17:38

11. plz send me 1 ex that will explain how to use connection pooling using MySQl
View Tutorial          By: abhijit marne at 2011-12-09 06:32:07

12. Very good explanation but i need to know how the connection pooing is configure in various servers p
View Tutorial          By: surender at 2011-11-17 11:15:22

13. it is super
View Tutorial          By: vinod at 2011-09-03 04:06:02

14. super explaination
View Tutorial          By: santhish at 2011-07-06 05:00:54

15. Good explanation, Thanks.
View Tutorial          By: bram at 2011-05-06 03:37:54

16. please add code snippets.
View Tutorial          By: jan at 2010-07-27 18:49:53

17. Hi Goose,
i want to know how the connection pooling work with java and jdbc. i want the exact

View Tutorial          By: Periyasamy at 2010-06-17 06:08:01

18. hai,
i want to know how the connection pooling work with java and jdbc. i want the ex

View Tutorial          By: V.Selvi at 2010-02-28 21:01:33

19. Hello,

Could you please include all the relevant files including the Tomcat files to

View Tutorial          By: John at 2007-12-19 04:35:54

Most Viewed Articles (in JDBC )

Latest Articles (in JDBC)

Comment on this tutorial