// actual pool of connections.
//
// We'll use a GenericObjectPool instance, although
// any ObjectPool implementation will suffice.
//
ObjectPool connectionPool = new GenericObjectPool(null);
//
// Next, we'll create a ConnectionFactory that the
// pool will use to create Connections.
// We'll use the DriverManagerConnectionFactory,
// using the connect string passed in the command line
// arguments.
//
//ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, "admin_user", "client00");
//
// Now we'll create the PoolableConnectionFactory, which wraps
// the "real" Connections created by the ConnectionFactory with
// the classes that implement the pooling functionality.
//
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
//
// Finally, we create the PoolingDriver itself...
//
PoolingDriver driver = new PoolingDriver();
//
// ...and register our pool with it.
//
driver.registerPool("example",connectionPool);
//
// Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
// to access our pool of Connections.
//
System.out.println("Setting up driver: Done.");
//
// Now, we can use JDBC as we normally would.
// Using the connect string
// jdbc:apache:commons:dbcp:example
// The general form being:
// jdbc:apache:commons:dbcp:<name-of-pool>
//
Connection conn = null;
PreparedStatement pstmt = null;
Statement stmt = null;
ResultSet rset = null;
// Check to see if connection is pooled
long startTime = new Date().getTime();
long currentTime;
System.out.println("Creating connection.");
try {
conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example");
conn.setAutoCommit(false);
System.out.println("Connection time=" + (new Date().getTime() - startTime));
System.out.println("Creating statement.");
// Clean table
stmt = conn.createStatement();
rset = stmt.executeQuery("delete from Test;");
sql = "insert into APP.Test " +
"set TestID=? " +
", TextField=?";
pstmt = conn.prepareStatement(sql);
System.out.println("Executing statement.");
} catch(SQLException e) {
e.printStackTrace();
try { pstmt.close(); } catch(Exception e1) { }
try { conn.close(); } catch(Exception e2) { }
}
for (int n=1; n <101; n++) {
System.out.println("=======");
System.out.println("n=" + n);
try {
pstmt.setInt(1, n);
pstmt.setString(2, "This is field " + n);
rset = pstmt.executeQuery();
} catch(SQLException e) {
e.printStackTrace();
try { pstmt.close(); } catch(Exception e2) { }
try { conn.close(); } catch(Exception e2) { }
}
// Reset time
startTime = new Date().getTime();
}
try {
conn.commit();
} catch(SQLException e) {
e.printStackTrace();
}
try { pstmt.close(); } catch(Exception e2) { }
try { conn.close(); } catch(Exception e2) { }
System.out.println("DBCP example: sql done");
// Close connection pool
try {
connectionPool.close();
} catch (Exception e) {
System.out.println("Exception thrown closing connection pool:");
System.out.println(e.getMessage());
}