Understanding Connection Pooling

1. Introduction

Connection Pooling is a technique to boost application’s performance where N connections to the database are opened and managed in a pool. The application just asks for a connection, uses it and then drops it back to the pool. When the application demands for a connection, the ready connections are kept available to used in the pool. The pool manages the connection lifecycle, such that the developer actually doesn’t need to wait for the connection to get established and filter out the stale ones.

Connection Pooling mechanism saves from the expensive operation to establish network connection at application-runtime and eventually initialize the database session at the backend.

2. Connection Pool implementation in Tomcat

Tomcat, the leading application server, is packaged with Commons DBCP Datasource as the default JNDI Datasource, unless we explicitly specify the DataSourceFactory as –

To use Apache Commons DBCP connection pool

To use Tomcat JDBC Connection pool

factory is required, and type should always be javax.sql.DataSource


The beauty of DBCP is that it can be used with so many applications or frameworks and it works with almost all databases in the market.

3. Comparing Pooling Mechanisms

When using Commons DBCP connection pool mechanism, Tomcat fetches the Commons DBCP sources (the version depends on the Tomcat version, for instance Tomcat 7.0.27 uses Commons DBCP 1.4), and does package name replacement (org.apache.commons -> org.apache.tomcat.dbcp) and builds the result as tomcat-dbcp.jar. This is done so that the internal Tomcat JDBC pools never conflict with possible application uses of the Commons DBCP classes. This avoids many potential classloading issues. The “dbcp” packages are about datasource management.

Similarly, Tomcat JDBC connection pool mechanism (org.apache.tomcat.jdbc.pool.*) is an alternative upgraded implementation of a database connection pool that competes with Apache Commons DBCP. It is a separate project from Tomcat (Under Tomcat Modules -> jdbc-pool), but it debuted with Tomcat 7 (starting with 7.0.19 in July 2011). There are pros and cons of both implementations, however Apache Commons DBCP is still being more popularly used.

In either case, the respective JARs (commons-dbcp.jar or tomcat-jdbc.jar) need to be included into the build path.

Older versions of Apache Commons DBCP (i.e. version 1.2) had some nasty thread-safety issues under high load conditions, making it unsuitable for that kind of usage, which was why the Tomcat JDBC Connection pool was re-written.

The usage of Tomcat JDBC Connection Pool is also very simple and for people who are already familiar with DBCP, the transition is quite very simple.

4. Connection Pooling in a Hibernate based application

Hibernate uses its magic to identify which connection pool provider to use — based on the properties you configure.

For c3p0 –

For Apache Commons DBCP –

The corresponding JAR file needs to be in place in the lib directory, manually or using Maven.

We can also explictly specify the connection provider with the hibernate.connection.provider_class property, though its not actually needed.

If we do not configure a connection pool with Hibernate, the default is used. It is visible in the log or console output when we start the application –

The default connection pool implementation in Hibernate is NOT suitable for production use, primarily because of it’s configuration constraints.

If you are using an application server, you may wish to use the built-in pool (typically a connection is obtained using JNDI).

To use the server’s built-in pool with Hibernate using JNDI configuration, we need to set the following property into the Hibernate configuration file –

– assuming TestDB is the JNDI name of Tomcat JDBC Connection Pool DataSource (refer snippet in Section 2 above).

If you can’t or don’t wish to use your application server’s built-in connection pool, Hibernate supports several other connection pools such as –

  1. c3p0
  2. Proxool

After Apache DBCP, the second most preferred connection pool implementation is c3p0, which easily integrates with Hibernate, and is said to deliver good performance.

5. Useful Links

How to configure the C3P0 connection pool in Hibernate

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.