JDBC for SQL Server - Herong's Tutorial Examples - v3.14, 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:sqlserver://localhost\\SQLEXPRESS;" + "user=herong;password=T0pSecret"; // 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 SQL Server server, I need SQL Server 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:\ mssql-jdbc-9.4.1.jre16.jar \ DbcpPoolingDriverTest.java DBCP PoolingDriver Info: Active Connections: 1 Idle Connections: 0 Database Connection Info: Server name: Microsoft SQL Server Server version: 15.00.2000 Driver name: Microsoft JDBC Driver 9.4 for SQL Server Driver version: 9.4.1.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 SQL Server server, log in as "root". I see 1 connection as expected.
1> SELECT loginame, COUNT(dbid) FROM sys.sysprocesses 2> GROUP BY dbid, loginame; 3> GO loginame ------------------------------------ ------ DESKTOP\Admin 1 Herong 1 NT SERVICE\SQLTELEMETRY$SQLEXPRESS 1 sa 13 sa 39 (5 rows affected)
So, what we can see from this tutorial:
Table of Contents
JDBC (Java Database Connectivity) Introduction
Microsoft SQL Server Express Edition
Microsoft JDBC Driver for SQL Server
Microsoft JDBC Driver - Query Statements and Result Sets
Microsoft JDBC Driver - DatabaseMetaData Object
Microsoft JDBC Driver - DDL Statements
Microsoft JDBC Driver - DML Statements
SQL Server - PreparedStatement
SQL Server CLOB (Character Large Object) - TEXT
SQL Server 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
Connection Pooling with C3P0 DataSources
JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver
JDBC-ODBC Bridge Driver - Flat Text Files
JDBC-ODBC Bridge Driver - MS Access