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, DbcpPoolingDriverTest.java.

/* DbcpPoolingDriverTest.java
 * Copyright (c) HerongYang.com. 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 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";

    // Setup a new PoolingDriver
    setupDriver(url);

    // Get a pooled connection from the new PoolingDriver
    Connection con = 
      DriverManager.getConnection("jdbc:apache:commons:dbcp:HyPool");
    checkDriver();

    // 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();
    checkDriver();

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

    // Destroy the PoolingDriver
    destroyDriver();
  }

  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
    pcf.setPool(op);

    // Load DBCP's PoolingDriver
    Class.forName("org.apache.commons.dbcp2.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:");
    driver.closePool("HyPool");
  }

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

Let's run it:

herong> java -cp .:\
  commons-dbcp2-2.8.0.jar:\
  commons-pool2-2.10.0.jar:\
  commons-logging-1.2.jar:\
  mysql-connector-java-8.0.27.jar \
  DbcpPoolingDriverTest.java

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

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

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

Go and count connections on the database server ...

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

mysql> show processlist;
+----+--------+-----------------+----------+---------+--------+
| Id | User   | Host            | db       | Command | Time   |
+----+--------+-----------------+----------+---------+--------+
| 93 | Herong | localhost:55070 | herongdb | Sleep   |     15 |
+----+--------+-----------------+----------+---------+--------+

So, what we can see from this tutorial:

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 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

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