JDBC for Oracle - Herong's Tutorial Examples - v3.13, by Herong Yang
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:oracle:thin:Herong/TopSecret@//localhost:1521/XE"; // 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 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 .:\ commons-dbcp2-2.8.0.jar:\ commons-pool2-2.10.0.jar:\ commons-logging-1.2.jar:\ ojdbc11.jar \ DbcpPoolingDriverTest.java 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: 21.4.0.0.0 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; SID SUBSTR(USERNAME,0,8) ---------- -------------------------------- 379 SYS 507 HERONG 2 rows selected.
So, what we can see from this tutorial:
Table of Contents
JDBC (Java Database Connectivity) Introduction
Oracle Express Edition Installation on Windows
Oracle - Reference Implementation of JdbcRowSet
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
Connection Pooling with C3P0 ComboPooledDataSource