How connection pooling works in Java and JDBC

By: Ramlak Emailed: 1592 times Printed: 2037 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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


JDBC Home | All JDBC Tutorials | Latest JDBC Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(14)


1. View Comment

Hello,

Could you please include all the relevant files including the Tomcat files to complete this tutorial?

Thanks
John
www.listpropertiesnow.com


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

hai,
i want to know how the connection pooling work with java and jdbc. i want the exact codings how the single connection pool is working and also want how to create the connection pool in java?


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

please add code snippets.

View Tutorial          By: jan at 2010-07-27 18:49:53
4. View Comment

Good explanation, Thanks.

View Tutorial          By: bram at 2011-05-06 03:37:54
5. View Comment

super explaination

View Tutorial          By: santhish at 2011-07-06 05:00:54
6. View Comment

it is super

View Tutorial          By: vinod at 2011-09-03 04:06:02
7. View Comment

Very good explanation but i need to know how the connection pooing is configure in various servers pls provide the info


Regards
surender


View Tutorial          By: surender at 2011-11-17 11:15:22
8. View Comment

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
9. View Comment

If the database server goes down, should the connection pool be refreshed. If conection pool is not refreshed, can the client use the existing connection object and try to connect successfully. Will this result in winsoc error.

View Tutorial          By: Naveen Gowd at 2012-02-02 07:17:38
10. View Comment

Assume scenario:
- i have set connection pool size as 5 in my application.
- when 5 users access my app all 5 connections will be utilized from the pool.

What if user 6 access the app & still all 5 users are currently using the app.
My understanding is 1 connection will be used by 1 thread/user. Correct me if i'm wrong.

Thanks,
Dan


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

good explanation .... Thanks

View Tutorial          By: nazeer at 2012-05-24 09:18:19
12. View Comment

if suppose i am having connection pool max size 30 and 30 connections are active for 1 hr then, what will happen with other connections(excluing active first 30th connections) which are waiting....will they wait for 1 hr? And one more question is what will happen if we not give max connection size?

View Tutorial          By: Shrikant at 2012-07-20 12:09:33
13. View Comment

Please add the code snippet ..

View Tutorial          By: mari selvam at 2012-08-23 10:13:51
14. View Comment

1. Performance

Object pooling provides better application performance As object creation is not done when client actually need it to perform some operation on it Instead objects are already created in the pool and readily available to perform any time. So Object creation activity is done much before So it does help in achieving better run-time performance

2. Object sharing :

Object Pooling encourage the concept of sharing. Objects available in pool can be shared among multiple worker threads . One thread Use the Object and once used it returns back to its Object pool and then it can be used by some other worker thread. So once created objects are not destroyed and thus destruction and creation again and again is not required. That again help in generating better performing code.

3. Control on Object instances :
By declaring size of Object pool we can control the no of instance creation. Thus a finite no of objects are created as decided depending upon required application capacity and scalability or peak load.

4. Memory conservation :
Finite no of instances are created So it helps in better memory management . Too many instances are not

Read through extensive details here :

http://efectivejava.blogspot.in/2013/09/8-reasons-why-object-pooling-is.html


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

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ramlak
While Loop in VB.net
For Each…Next Loop in VB.net
For Loop in VB.net
Do Loop in VB.net
Setting Up SSL on Tomcat
Unicode and UTF-8 in C
Sample program to demonstrate the use of ActionListener
java.io.IOException: HTTPS hostname wrong: should be
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
NotifyUtil::java.net.ConnectException: Connection refused: connect
'LINK.EXE' is not recognized as an internal or ext
Using Transactions in JDBC
What is the ACID principal?
How connection pooling works in Java and JDBC
A simple JDBC application sample code

More Tutorials in JDBC
TEXT datatype SPLIT in MSSQL - to solve the 8000 limit set by varchar
Import TEXT to TABLE in MSSQL
What is Referential Integrity in databases?
Handling CSV in Stored Procedures
setSavepoint and releaseSavepoint Example in Java
Calling a Stored Procedure from JDBC in Java
java.lang.NoClassDefFoundError and java.lang.NoSuchMethodError
Creating Database Connection Pool in Tomcat 5.0 and Tomcat 5.5 for MySQL and Java
JDBC Basics and JDBC Components
SELECT Statements
WHERE Clauses in SQL
Joins example in SQL
Common SQL Commands
Result Sets, Cursors and Transactions in SQL
Stored Procedures example in SQL

More Latest News
Most Viewed Articles (in JDBC )
A simple JDBC application sample code
How connection pooling works in Java and JDBC
JDBC Components
Using JDBC to extract data from a database and output to an XML document
Using the DriverManager Class vs Using a DataSource Object for a connection
PreparedStatement Example in Java
Using JDBC to connect to MySQL from Java Program
The Structure of JDBC
WHERE Clauses in SQL
JDBC Architecture
What is the ACID principal?
Calling a Stored Procedure from JDBC in Java
JDBC Basics and JDBC Components
Data Access Technologies in Java
Creating Database Connection Pool in Tomcat 5.0 and Tomcat 5.5 for MySQL and Java
Most Emailed Articles (in JDBC)
The Structure of JDBC
A simple JDBC application sample code
JDBC Components
What is the ACID principal?
Using Transactions in JDBC
How connection pooling works in Java and JDBC
Using JDBC to extract data from a database and output to an XML document
Using JDBC to connect to MySQL from Java Program
JDBC and Tomcat context settings
PreparedStatement Example in Java
Result Sets, Cursors and Transactions in SQL
Data Access Technologies in Java
WHERE Clauses in SQL
Joins example in SQL
Common SQL Commands