Showing posts with label connection pool. Show all posts
Showing posts with label connection pool. Show all posts

Wednesday, October 03, 2012

Why C3P0 over Commons DBCP?

What is Connection pool?
Connection pool is good for performance, as it prevents Java application create a connection each time when interact with database and minimizes the cost of opening and closing connections. The purpose is to reuse the connections and manage the process of opening, closing efficiently.

The shortcomings/disadvantages of Commons DBCP are very clearly documented over Tomcat 7.0 JDBC connection pool  documentation.

  1. commons-dbcp is single threaded, in order to be thread safe commons-dbcp locks the entire pool, even during query validation.
  2. commons-dbcp is slow - as the number of logical CPUs grow, the performance suffers, the above point shows that there is not support for high concurrency Even with the enormous optimizations of the synchronized statement in Java 6, commons-dbcp still suffers in speed and concurrency.
  3. commons-dbcp is complex, over 60 classes. tomcat-jdbc-pool, core is 8 classes, hence modifications for future requirement will require much less changes. This is all you need to run the connection pool itself, the rest is gravy.
  4. commons-dbcp uses static interfaces. This means you can't compile it with JDK 1.6, or if you run on JDK 1.6/1.7 you will get NoSuchMethodException for all the methods not implemented, even if the driver supports it.
  5. The commons-dbcp has become fairly stagnant. Sparse updates, releases, and new feature support.
  6. It's not worth rewriting over 60 classes, when something as a connection pool can be accomplished with as a much simpler implementation.
  7. Tomcat jdbc pool implements a fairness option not available in commons-dbcp and still performs faster than commons-dbcp
  8. Tomcat jdbc pool implements the ability retrieve a connection asynchronously, without adding additional threads to the library itself
  9. Tomcat jdbc pool is a Tomcat module, it depends on Tomcat JULI, a simplified logging framework used in Tomcat.
  10. Retrieve the underlying connection using the javax.sql.PooledConnection interface.
  11. Starvation proof. If a pool is empty, and threads are waiting for a connection, when a connection is returned, the pool will awake the correct thread waiting. Most pools will simply starve.


See my other post (Tomcat 7: C3P0 Datasource Pool) summarizing how to Configure C3P0 in Tomcat 7.

Tomcat 7: C3P0 Datasource Pool

Goal:

To configure C3P0 datasource pool in tomcat 7

Detail: 

Till now I was using DBCP JDBC connection pooling for my applications. Considering that the library seems to be out of date now and is not meant for production grade system with heavy load on them, I looked towards the other free open source connection pooling library C3P0.

C3P0 is an easy-to-use library for making traditional JDBC drivers "enterprise-ready" by augmenting them with functionality defined by the jdbc3 spec and the optional extensions to jdbc2.
Below are the steps to configure C3P0 pool in tomcat. 
  1. Download the latest c3p0-{version}.jar from http://sourceforge.net/projects/c3p0/.
  2. Copy the c3p0-{version}.jar to tomcat/lib directory.
  3. Open the tomcat/conf/context.xml in edit mode and add the below lines inside <Context> element.
    <Resource auth="Container" description="DB Connection" driverClass="com.mysql.jdbc.Driver" maxPoolSize="50" minPoolSize="10" acquireIncrement="10" name="jdbc/MyDBPool" user="root" password="abc123" factory="org.apache.naming.factory.BeanFactory" type="com.mchange.v2.c3p0.ComboPooledDataSource" jdbcUrl="jdbc:mysql://localhost:3306/lportal?autoReconnect=true" />
     
  4.  For  more on configuring various attributes of the connection pool, see http://www.mchange.com/projects/c3p0/#configuration

Environment:

Java 6
Tomcat 7
MySQL 5

Reference:

See my other post (Why C3P0 over Commons DBCP?) for more on DBCP vs C3P0