![DBCP Pooling Driver Snippet DBCP Pooling Driver Snippet]()
Here's a simple example of how to use the PoolingDriver. In this example, we'll construct the PoolingDriver manually, just to show how the pieces fit together, but you could also configure it using an external conifguration file in JOCL format (and eventually Digester).<!--break-->
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
publicclass ManualPoolingDriverExample {
publicstaticvoid main(String[] args) {
System.out.println("Loading underlying JDBC driver.");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Done.");
System.out.println("Setting up driver.");
try {
setupDriver(args[0]);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Done.");
Connection conn =null;
Statement stmt =null;
ResultSet rset =null;
try {
System.out.println("Creating connection.");
conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example");
System.out.println("Creating statement.");
stmt = conn.createStatement();
System.out.println("Executing statement.");
rset = stmt.executeQuery(args[1]);
System.out.println("Results:");
int numcols = rset.getMetaData().getColumnCount();
while(rset.next()) {
for(int i=1;i<=numcols;i++) {
System.out.print("\t"+ rset.getString(i));
}
System.out.println("");
}
} catch(SQLException e) {
e.printStackTrace();
} finally {
try { if (rset !=null) rset.close(); } catch(Exception e) { }
try { if (stmt !=null) stmt.close(); } catch(Exception e) { }
try { if (conn !=null) conn.close(); } catch(Exception e) { }
}
try {
printDriverStats();
} catch (Exception e) {
e.printStackTrace();
}
try {
shutdownDriver();
} catch (Exception e) {
e.printStackTrace();
}
}
publicstaticvoid setupDriver(String connectURI) throws Exception {
ObjectPool connectionPool =new GenericObjectPool(null);
ConnectionFactory connectionFactory =new DriverManagerConnectionFactory(connectURI,null);
PoolableConnectionFactory poolableConnectionFactory =new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
Class.forName("org.apache.commons.dbcp.PoolingDriver");
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
driver.registerPool("example",connectionPool);
}
publicstaticvoid printDriverStats() throws Exception {
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
ObjectPool connectionPool = driver.getConnectionPool("example");
System.out.println("NumActive: "+ connectionPool.getNumActive());
System.out.println("NumIdle: "+ connectionPool.getNumIdle());
}
publicstaticvoid shutdownDriver() throws Exception {
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
driver.closePool("example");
}
}