C3P0 Connection Pooling Java Example

In this post we’ll see how to configure connection pooling in your application using C3P0.

Maven dependency for MChange C3P0

If you are using Maven then you can add the following dependency in pom.xml


You can also download the jars directly from the following location and put them in application’s classpath.

Jars you need to have in classpath are lib/c3p0- and lib/mchange-commons-java-0.2.19.jar

C3P0 Connection pooling Java example

In the example DB used is MySQL it connects to knpcode schema and table is EMPLOYEE with columns as id, FIRST_NAME, LAST_NAME and DEPARTMENT.

Best way to create a c3p0 pooling DataSource is to instantiate an instance of ComboPooledDataSource class and provide the properties for connecting to DB and for connection pool.

DB related configurations that you need to set are Driver class, URL, user name and password.

connection pool related configuration out of the following-

  • acquireIncrement– Determines how many Connections a c3p0 pool will attempt to acquire when the pool has run out of Connections. Default is 3.
  • initialPoolSize– Number of Connections a pool will try to acquire upon startup. Default is 3.
  • maxPoolSize– Maximum number of Connections a pool will maintain at any given time. Default is 15.
  • maxIdleTime– Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire. Default is 0.
  • minPoolSize– Minimum number of Connections a pool will maintain at any given time. Default is 3.

DB credentials and connection pool related configuration properties are kept in a properties file (db.properties).


Following class is used to create a ComboPooledDataSource.

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DSCreator {
  private static ComboPooledDataSource pooledDS;
  static {
    try {
    	pooledDS = new ComboPooledDataSource();
      Properties properties = new Properties();
      // Loading properties file from classpath
      InputStream inputStream = DSCreator.class
      if(inputStream == null){
        throw new IOException("File not found");

      // Default anyway
    }catch(IOException | PropertyVetoException e) {
  public static DataSource getDataSource() {
    return pooledDS;

Test class that creates a connection using the returned DataSource object and queries the DB.

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;

public class DSDemo {
  public static void main(String[] args) {
    DSDemo dsDemo = new DSDemo();
  private void displayEmployeeById(int id){
    Connection connection = null; 
    String selectSQL = "SELECT * FROM EMPLOYEE WHERE id = ?";
    PreparedStatement prepStmt = null;
    try {
      DataSource ds = DSCreator.getDataSource();
      connection = ds.getConnection();
      prepStmt = connection.prepareStatement(selectSQL);
      prepStmt.setInt(1, id);
      ResultSet rs = prepStmt.executeQuery();
        System.out.println("id: " + rs.getInt("id"));
        System.out.println("First Name: " + rs.getString("FIRST_NAME"));
        System.out.println("Last Name: " + rs.getString("LAST_NAME"));
        System.out.println("Department: " + rs.getString("DEPARTMENT"));
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      if(connection != null){
        try {
        } catch (SQLException e) {
          // TODO Auto-generated catch block

Related Posts

That’s all for the topic C3P0 Connection Pooling Java Example. If something is missing or you have something to share about the topic please write a comment.

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.