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:oracle:thin:Herong/TopSecret@//localhost:1521/XE";

    // 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) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
    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) DriverManager.getDriver("jdbc:apache:commons:dbcp:");

  public static void checkDriver() throws Exception {
    PoolingDriver driver =
      (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
  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 Oracle server, I need Oracle JDBC, DBCP, and 2 other supporting JAR files. See other tutorials to download them.

Let's run it:

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

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

Database Connection Info:
   Server name: Oracle
   Server version: Oracle Database 21c Express Edition Release ...
   Driver name: Oracle JDBC driver
   Driver version:
   JDBC major version: 4
   JDBC minor version: 3

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

Go and count connections on the database server ...

On the Oracle server, log in as "root". I see 1 connection as expected.

SQL> SELECT sid, SUBSTR(username,0,8) FROM v$session 
       WHERE username IS NOT NULL;

---------- --------------------------------
       379 SYS
       507 HERONG

2 rows selected.

So, what we can see from this tutorial:

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 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

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