Connection Pooling with C3P0 ComboPooledDataSource

This section provides a tutorial example that shows you how to use the C3P0 ComboPooledDataSource class to manage the connection pool with JavaBean style properties.

The easiest way to use C3P0 package for connection pooling is to use the com.mchange.v2.c3p0.ComboPooledDataSource class. It implements the standard JDBC javax.sql.DataSource interface to manage the connection pool with JavaBean style properties.

The ComboPooledDataSource class uses set*() and get*() methods to configure and manage the connection pool. Here is my sample Java program, C3p0ComboPooledDataSourceExample.java against my local MySQL server.

/* C3p0ComboPooledDataSourceExample.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0ComboPooledDataSourceExample {
  static java.io.PrintStream out = System.out;

  public static void main(String[] args) throws Exception { 
    String url = "jdbc:mysql://localhost:3306/HerongDB?"
               + "user=Herong&password=TopSecret";

    // Get a DataSource with pooled connections
    DataSource ds = getDataSource(url);

    // Get a pooled connection
    Connection con = ds.getConnection();
    checkDataSource(ds);

    // Verify the pooled connection
    DatabaseMetaData meta = con.getMetaData();
    out.println();
    out.println("Database Connection Info: ");
    out.println("   Server name: "+meta.getDatabaseProductName());
    out.println("   Server version: "+meta.getDatabaseProductVersion());
    out.println("   Driver name: "+ meta.getDriverName());
    out.println("   Driver version: "+ meta.getDriverVersion());
    out.println("   JDBC major version: "+ meta.getJDBCMajorVersion());
    out.println("   JDBC minor version: "+ meta.getJDBCMinorVersion());

    // Close the pooled connection 
    con.close();

    // Wait for 3 minutes to check the server side
    out.println();
    out.println("Go and count connections on the database server ...");
    Thread.sleep(3*60*1000);

    // Close the DataSource
    closeDataSource(ds);
  }

  public static DataSource getDataSource(String url) throws Exception {
    ComboPooledDataSource cpds = new ComboPooledDataSource();
    cpds.setJdbcUrl(url);
    cpds.setInitialPoolSize(10);
    DataSource ds = (DataSource) cpds;
    return ds;
  }

  public static void closeDataSource(DataSource ds) throws Exception {
    ComboPooledDataSource cpds = (ComboPooledDataSource) ds;
    cpds.close();
  }

  public static void checkDataSource(DataSource ds) throws Exception {
    ComboPooledDataSource cpds = (ComboPooledDataSource) ds;
    out.println();
    out.println("C3P0 ComboPooledDataSource Info: ");
    out.println("   Total Connections: "+cpds.getNumConnections());
    out.println("   Busy Connections: "+cpds.getNumBusyConnections());
    out.println("   Idle Connections: "+cpds.getNumIdleConnections());
    out.println("   Connection URL: "+cpds.getJdbcUrl());
    out.println("   User Name: "+cpds.getUser());
    out.println("   Password: "+cpds.getPassword());
  }
}

To run this test program, I need both MySQL JDBC and C3P0 JAR files. See other tutorials to download them.

Let's run it:

herong> java -cp .:\
  c3p0-0.9.5.5.jar:\
  mysql-connector-java-8.0.27.jar \
  C3p0ComboPooledDataSourceExample.java 

8:37:33 PM com.mchange.v2.log.MLog 
INFO: MLog clients using java 1.4+ standard logging.
8:37:38 PM com.mchange.v2.c3p0.C3P0Registry 
INFO: Initializing c3p0-0.9.5.5 [built 11-December-2019 22:07:46 ...]
8:37:38 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource 
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource 
  [ acquireIncrement -> 3, 
    acquireRetryAttempts -> 30, 
    ...
  ]

C3P0 ComboPooledDataSource Info: 
   Total Connections: 3
   Busy Connections: 1
   Idle Connections: 2
   Connection URL: jdbc:mysql://localhost:3306/HerongDB?user=Herong...
   User Name: null
   Password: null

Database Connection Info: 
   Server name: MySQL
   Server version: 8.0.17
   Driver name: MySQL Connector/J
   Driver version: mysql-connector-java-8.0.19 (Revision: ...)
   JDBC major version: 4
   JDBC minor version: 2

Go and count connections on the database server ...

On the MySQL server, log in as "root". I see 10 connections:

mysql> show processlist;
+-----+--------+-----------------+----------+---------+--------+
| Id  | User   | Host            | db       | Command | Time   |
+-----+--------+-----------------+----------+---------+--------+
| 108 | Herong | localhost:57094 | herongdb | Sleep   |      6 |
| 109 | Herong | localhost:57093 | herongdb | Sleep   |      6 |
| 110 | Herong | localhost:57095 | herongdb | Sleep   |      6 |
| 111 | Herong | localhost:57097 | herongdb | Sleep   |      6 |
| 112 | Herong | localhost:57096 | herongdb | Sleep   |      6 |
| 113 | Herong | localhost:57098 | herongdb | Sleep   |      6 |
| 114 | Herong | localhost:57100 | herongdb | Sleep   |      6 |
| 115 | Herong | localhost:57101 | herongdb | Sleep   |      6 |
| 116 | Herong | localhost:57099 | herongdb | Sleep   |      6 |
| 117 | Herong | localhost:57102 | herongdb | Sleep   |      6 |
+-----+--------+-----------------+----------+---------+--------+

So, what we can see from this tutorial:

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

 Java DB (Derby) - DML Statements

 Java DB (Derby) - ResultSet Objects of Queries

 Java DB (Derby) - PreparedStatement

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

 MySQL - PreparedStatement

 MySQL - Reference Implementation of JdbcRowSet

 MySQL - JBDC CallableStatement

 MySQL CLOB (Character Large Object) - TEXT

 MySQL BLOB (Binary Large Object) - BLOB

 Oracle Express Edition Installation on Windows

 Oracle JDBC Drivers

 Oracle - Reference Implementation of JdbcRowSet

 Oracle - PreparedStatement

 Oracle - JBDC CallableStatement

 Oracle CLOB (Character Large Object) - TEXT

 Oracle BLOB (Binary Large Object) - BLOB

 Microsoft SQL Server Express Edition

 Microsoft JDBC Driver for SQL Server

 Microsoft JDBC Driver - Query Statements and Result Sets

 Microsoft JDBC Driver - DatabaseMetaData Object

 Microsoft JDBC Driver - DDL Statements

 Microsoft JDBC Driver - DML Statements

 SQL Server - PreparedStatement

 SQL Server CLOB (Character Large Object) - TEXT

 SQL Server BLOB (Binary Large Object) - BLOB

 JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver

 JDBC-ODBC Bridge Driver - Flat Text Files

 JDBC-ODBC Bridge Driver - MS Access

 JDBC-ODBC Bridge Driver - MS SQL Server

 Summary of JDBC Drivers and Database Servers

Using Connection Pool with JDBC

 What Is Database Connection Pool

 Commons DBCP for Connection Pooling

 Connection Pooling with Commons DBCP BasicDataSource

 Connection Pooling with Commons DBCP PoolingDriver

 C3P0 for Connection Pooling

Connection Pooling with C3P0 ComboPooledDataSource

 Connection Pooling with C3P0 DataSources

 Archived Tutorials

 References

 Full Version in PDF/EPUB