Connection Pooling with Commons DBCP BasicDataSource

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

The easiest way to use Commons DBCP package for connection pooling is to use the org.apache.commons.dbcp2.BasicDataSource class. It implements the standard JDBC javax.sql.DataSource interface to manage the connection pool with JavaBean style properties.

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

/* DbcpBasicDataSourceTest.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;

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

  public static void main(String[] args) throws Exception { 
    String tzid = java.util.TimeZone.getDefault().getID();
    String url = "jdbc:mysql://localhost:3306/HerongDB?"
      + "user=Herong&password=TopSecret&serverTimezone="+tzid;

    // 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 {
    BasicDataSource bds = new BasicDataSource();
    bds.setUrl(url);
    bds.setInitialSize(10);
    DataSource ds = (DataSource) bds;
    return ds;
  }

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

  public static void checkDataSource(DataSource ds) {
    BasicDataSource bds = (BasicDataSource) ds;
    out.println();
    out.println("DBCP BasicDataSource Info: ");
    out.println("   Active Connections: " + bds.getNumActive());
    out.println("   Idle Connections: " + bds.getNumIdle());
    out.println("   Connection URL: " + bds.getUrl());
    out.println("   Driver Class: " + bds.getDriverClassName());
    out.println("   User Name: " + bds.getUsername());
    out.println("   Password: " + bds.getPassword());
  }
}

To run this test program, I need 4 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 \
  DbcpBasicDataSourceTest.java

DBCP BasicDataSource Info: 
   Active Connections: 1
   Idle Connections: 7
   Connection URL: jdbc:mysql://localhost:3306/HerongDB?user=Herong...
   Driver Class: null
   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 8 connections:

mysql> show processlist;
+----+--------+-----------------+----------+---------+--------+
| Id | User   | Host            | db       | Command | Time   |
+----+--------+-----------------+----------+---------+--------+
| 73 | Herong | localhost:52781 | herongdb | Sleep   |     94 |
| 74 | Herong | localhost:52782 | herongdb | Sleep   |     94 |
| 75 | Herong | localhost:52783 | herongdb | Sleep   |     94 |
| 76 | Herong | localhost:52784 | herongdb | Sleep   |     94 |
| 77 | Herong | localhost:52785 | herongdb | Sleep   |     94 |
| 78 | Herong | localhost:52786 | herongdb | Sleep   |     94 |
| 79 | Herong | localhost:52787 | herongdb | Sleep   |     94 |
| 80 | Herong | localhost:52788 | herongdb | Sleep   |     94 |
+----+--------+-----------------+----------+---------+--------+

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