//
// We'll use a GenericObjectPool instance, although
// any ObjectPool implementation will suffice.
//
//ObjectPool connectionPool = new GenericObjectPool(null);
GenericObjectPool connectionPool = new GenericObjectPool(null);
connectionPool.setMaxActive(-1);
//
// 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);
String url;
String sql;
boolean serverMode = true;
if (serverMode) {
url = "jdbc:mckoi://localhost";
} else {
url = "jdbc:mckoi:local://resource/nz.co.transparent.client.db.conf";
}
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;
boolean closeConnection = false;
long startTime;
long currentTime;
// DBCP can handle a maximum of 8 connections
// DBCP hangs at connection number 9
int loopCount = 20;
System.out.println("==> START");
for (int i=1; i<=loopCount; i++) {
System.out.println("==> i = " + i);
startTime = new Date().getTime();
try {
conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example");
conn.setAutoCommit(false);
System.out.println("Connection time=" + (new Date().getTime() - startTime));
System.out.println("Executing statement.");
sql = "select * from Client where (ClientID=1)";
stmt = conn.createStatement();
rset = stmt.executeQuery(sql);
rset.next();
System.out.println("LastName=" + rset.getString("LastName"));
if (closeConnection) {
try {stmt.close();
} catch(Exception e1) {
System.out.println("Error closing statement: " + e1.getMessage());
return;
}
try {conn.close();
} catch(Exception e2) {
System.out.println("Error closing connection: " + e2.getMessage());
return;
}
}
} catch(SQLException e) {
e.printStackTrace();
try {stmt.close(); } catch(Exception e1) { }
try { conn.close(); } catch(Exception e2) { }
}
}
try { stmt.close(); } catch(Exception e2) { }
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());
}