Sunday, December 09, 2012

How does JDBC Connection Pooling work?

This post is an excerpt from the jtracer blog.


The Apache commons DBCP component provides Connection pooling for JDBC connections. In this blog I am going to use JTracer to reveal how DBCP works internally.

JDBC Primer

To connect to a database using JDBC a java.sql.Connection is needed. For creating a Connection, a JDBC driver needs to be loaded. Connection parameters like url and credentials should also be set. Once a Connection is obtained you can create a Statement to execute queries. After the Statement and Connection are used, they should be closed to clean up the resources. The DataSource API as part of the javax.sql package provides an alternate way to create connections.

Creation of Connections is an expensive process. It is better to use a connection pool that will re-use Connections for running queries rather than creating only new Connections. The DBCP project from Apache provides a BasicDataSource which also implements Connection pooling.


Continue reading at the jtracer blog.