import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
public class ConnectionPool {
private final String DBDriver = "com.mysql.jdbc.Driver ";
private final String schema = "examsystem ";
private final String user = "root ";
private final String password = "comeon ";
private final static String poolName = "connectionpool ";
private final String driverInfo
= "jdbc:mysql://localhost/ " + schema + "?user= " + user + "&password= " + password
+ "&useUnicode=true&characterEncoding=gb2312 ";
private static ConnectionPool connectionPool;
private ConnectionPool(){
System.out.println( "Loading underlying JDBC driver. ");
try {
Class.forName(DBDriver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println( "Done. ");
System.out.println( "Setting up driver. ");
try {
setupDriver( driverInfo );
} catch (Exception e) {
e.printStackTrace();
}
System.out.println( "Done. ");
}
public static ConnectionPool getInstance () {
if ( connectionPool == null) {
connectionPool = new ConnectionPool();
}
return connectionPool;
}
public Connection getConnection() {
System.out.println( "Creating connection. ");
try {
Connection conn = DriverManager.getConnection( "jdbc:apache:commons:dbcp:connectionpool ");
System.out.println( "得到一个新的连接:/r/n " + conn.toString());
return conn;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public static void setupDriver(String connectURI) throws Exception {
//
// First, we'll need a ObjectPool that serves as the
// actual pool of connections.
//
// We'll use a GenericObjectPool instance, although
// any ObjectPool implementation will suffice.
//GenericObjectPool(org.apache.commons.pool.PoolableObjectFactory factory,
// int maxActive,
// byte whenExhaustedAction,
// long maxWait,
// int maxIdle,
// int minIdle,
// boolean testOnBorrow,
// boolean testOnReturn,
// long timeBetweenEvictionRunsMillis,
// int numTestsPerEvictionRun,
// long minEvictableIdleTimeMillis,
// boolean testWhileIdle)
ObjectPool connectionPool = new GenericObjectPool(null,
100,
GenericObjectPool.WHEN_EXHAUSTED_GROW,
10000,
50);
//
// 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);
//
// 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...
//
Class.forName( "org.apache.commons.dbcp.PoolingDriver ");
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver( "jdbc:apache:commons:dbcp: ");
//
// ...and register our pool with it.
//
driver.registerPool(poolName,connectionPool);
//
// Now we can just use the connect string "jdbc:apache:commons:dbcp:example "
// to access our pool of Connections.
//
}
public static void printDriverStats() throws Exception {
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver( "jdbc:apache:commons:dbcp: ");
ObjectPool connectionPool = driver.getConnectionPool(poolName);
System.out.println( "NumActive: " + connectionPool.getNumActive());
System.out.println( "NumIdle: " + connectionPool.getNumIdle());
}
public static void shutdownDriver() throws Exception {
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver( "jdbc:apache:commons:dbcp: ");
driver.closePool(poolName);
}
}
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
public class ConnectionPool {
private final String DBDriver = "com.mysql.jdbc.Driver ";
private final String schema = "examsystem ";
private final String user = "root ";
private final String password = "comeon ";
private final static String poolName = "connectionpool ";
private final String driverInfo
= "jdbc:mysql://localhost/ " + schema + "?user= " + user + "&password= " + password
+ "&useUnicode=true&characterEncoding=gb2312 ";
private static ConnectionPool connectionPool;
private ConnectionPool(){
System.out.println( "Loading underlying JDBC driver. ");
try {
Class.forName(DBDriver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println( "Done. ");
System.out.println( "Setting up driver. ");
try {
setupDriver( driverInfo );
} catch (Exception e) {
e.printStackTrace();
}
System.out.println( "Done. ");
}
public static ConnectionPool getInstance () {
if ( connectionPool == null) {
connectionPool = new ConnectionPool();
}
return connectionPool;
}
public Connection getConnection() {
System.out.println( "Creating connection. ");
try {
Connection conn = DriverManager.getConnection( "jdbc:apache:commons:dbcp:connectionpool ");
System.out.println( "得到一个新的连接:/r/n " + conn.toString());
return conn;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public static void setupDriver(String connectURI) throws Exception {
//
// First, we'll need a ObjectPool that serves as the
// actual pool of connections.
//
// We'll use a GenericObjectPool instance, although
// any ObjectPool implementation will suffice.
//GenericObjectPool(org.apache.commons.pool.PoolableObjectFactory factory,
// int maxActive,
// byte whenExhaustedAction,
// long maxWait,
// int maxIdle,
// int minIdle,
// boolean testOnBorrow,
// boolean testOnReturn,
// long timeBetweenEvictionRunsMillis,
// int numTestsPerEvictionRun,
// long minEvictableIdleTimeMillis,
// boolean testWhileIdle)
ObjectPool connectionPool = new GenericObjectPool(null,
100,
GenericObjectPool.WHEN_EXHAUSTED_GROW,
10000,
50);
//
// 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);
//
// 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...
//
Class.forName( "org.apache.commons.dbcp.PoolingDriver ");
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver( "jdbc:apache:commons:dbcp: ");
//
// ...and register our pool with it.
//
driver.registerPool(poolName,connectionPool);
//
// Now we can just use the connect string "jdbc:apache:commons:dbcp:example "
// to access our pool of Connections.
//
}
public static void printDriverStats() throws Exception {
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver( "jdbc:apache:commons:dbcp: ");
ObjectPool connectionPool = driver.getConnectionPool(poolName);
System.out.println( "NumActive: " + connectionPool.getNumActive());
System.out.println( "NumIdle: " + connectionPool.getNumIdle());
}
public static void shutdownDriver() throws Exception {
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver( "jdbc:apache:commons:dbcp: ");
driver.closePool(poolName);
}
}