Wednesday, October 03, 2012

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 

No comments:

Post a Comment