Connection Pooling with C3P0 DataSources

This section provides a tutorial example that shows you how to use the C3P0 DataSources class to create and convert a unpooled DataSource to a pooled DataSource using PoolBackedDataSource with default configurations.

The another easy way to use C3P0 package for connection pooling is to use the com.mchange.v2.c3p0.DataSources class. It offers 2 statics methods to create and convert a unpooled DataSource to a pooled DataSource using PoolBackedDataSource with default configurations.

Here is my sample Java program, against my local Derby server.

 * Copyright (c) All Rights Reserved.
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.DataSources;
import com.mchange.v2.c3p0.PoolBackedDataSource;

public class C3p0DataSourcesExample {
  static out = System.out;

  public static void main(String[] args) throws Exception {
    String url = "jdbc:derby://localhost/TestDB";

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

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

    // Verify the pooled connection
    DatabaseMetaData meta = con.getMetaData();
    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

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

    // Close the DataSource

  public static DataSource getDataSource(String url) throws Exception {
    DataSource unpooled = DataSources.unpooledDataSource(url);
    DataSource pooled = DataSources.pooledDataSource(unpooled);
    return pooled;

  public static void closeDataSource(DataSource ds) throws Exception {

  public static void checkDataSource(DataSource ds) throws Exception {
    PoolBackedDataSource pbds = (PoolBackedDataSource) ds;
    out.println("C3P0 PoolBackedDataSource Info: ");
    out.println("   Total Connections: "+pbds.getNumConnections());
    out.println("   Busy Connections: "+pbds.getNumBusyConnections());
    out.println("   Idle Connections: "+pbds.getNumIdleConnections());

To run this test program, I need the Derby JDBC and C3P0 JAR files as shown in the previous tutorial.

Let's run it:

herong> java -cp .:\
  derbyclient.jar \

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

C3P0 PoolBackedDataSource Info: 
   Total Connections: 3
   Busy Connections: 1
   Idle Connections: 2

Database Connection Info: 
  Server name: Apache Derby
  Server version: - (1913217)
  Driver name: Apache Derby Network Client JDBC Driver
  Driver version: - (1853019)
  JDBC major version: 4
  JDBC minor version: 2

Go and count connections on the database server ...

Unfortunately, I could not find a way to verify how many connections has been created by this test program on the Derby server. But it seems to be working correctly.

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 Derby (Java DB)

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

 Derby (Java DB) - DML Statements

 Derby (Java DB) - ResultSet Objects of Queries

 Derby (Java DB) - PreparedStatement

 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


 Full Version in PDF/EPUB