Connection Pooling with Commons DBCP PoolingDriver

This section provides a tutorial example that shows you how to use the Commons DBCP PoolingDriver class to manage the connection pool as a custom JDBC driver.

Another option to use Commons DBCP package for connection pooling is to use the org.apache.commons.dbcp2.PoolingDriver class. It implements the standard JDBC javax.sql.Driver interface to manage the connection pool with a custom JDBC driver.

The steps to build a custom JDBC driver is shown in the setupDriver() methods in the following sample Java program,

 * Copyright (c) All Rights Reserved.
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;

import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
import org.apache.commons.dbcp2.PoolableConnectionFactory;
import org.apache.commons.dbcp2.PoolableConnection;
import org.apache.commons.dbcp2.PoolingDriver;

import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool;

public class DbcpPoolingDriverTest {
  static out = System.out;

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

    // Setup a new PoolingDriver

    // Get a pooled connection from the new PoolingDriver
    Connection con =

    // 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 ...");

    // Destroy the PoolingDriver

  public static void setupDriver(String url) throws Exception {

    // Get DBCP's ConnectionFactory based ConnectionFactory
    ConnectionFactory cf = new DriverManagerConnectionFactory(url);

    // Convert it to a PoolableConnectionFactory
    PoolableConnectionFactory pcf =
      new PoolableConnectionFactory(cf, null);

    // Create an ObjectPool with the PoolableConnectionFactory
    ObjectPool<PoolableConnection> op = new GenericObjectPool<>(pcf);

    // Set the factory's pool property to the owning pool

    // Load DBCP's PoolingDriver

    // Register the pool in the PoolingDriver
    PoolingDriver driver = (PoolingDriver)
    driver.registerPool("HyPool", op);

    // The pool is ready as new JDBC driver:
    // "jdbc:apache:commons:dbcp:HyPool"

  public static void destroyDriver() throws Exception {
    PoolingDriver driver = (PoolingDriver)

  public static void checkDriver() throws Exception {
    PoolingDriver driver = (PoolingDriver)
    ObjectPool<? extends Connection> op
      = driver.getConnectionPool("HyPool");
    out.println("DBCP PoolingDriver Info: ");
    out.println("   Active Connections: " + op.getNumActive());
    out.println("   Idle Connections: " + op.getNumIdle());

Again, to run this test program against the Derby server, I need Derby JDBC, DBCP, and 2 other supporting JAR files as shown in the previous tutorial.

Let's run it:

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

DBCP PoolingDriver Info: 
   Active Connections: 1
   Idle Connections: 0

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

DBCP PoolingDriver Info: 
   Active Connections: 0
   Idle Connections: 1

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. 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