public class SimpleDataSourceDemo { public static ComboPooledDataSource cpds = new ComboPooledDataSource("dk-c3p0-pool"); static { try { init(); } catch (PropertyVetoException e) { e.printStackTrace(); System.exit(0); } } private static void init() throws PropertyVetoException { cpds.setDriverClass(driverClass); //loads the jdbc driver cpds.setJdbcUrl( url ); cpds.setUser(username); cpds.setPassword(pwd); cpds.setMaxStatements( 180 ); // the settings below are optional -- c3p0 can work with defaults cpds.setMinPoolSize(5); cpds.setAcquireIncrement(5); cpds.setMaxPoolSize(20); cpds.setConnectionCustomizerClassName("db.pool.c3p0.MyConnectionCustomizer"); cpds.setPreferredTestQuery("select 1"); cpds.setTestConnectionOnCheckin(true); cpds.setTestConnectionOnCheckout(true); cpds.setIdentityToken("dk-identityToken"); } public static Connection getConnection(){ try { System.out.println("idleconn="+cpds.getNumIdleConnections()); System.out.println("numconn="+cpds.getNumConnections()); System.out.println("minPoolSize="+cpds.getMinPoolSize()); System.out.println("maxPoolSize="+cpds.getMaxPoolSize()); return cpds.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } public static void main(String[] args) throws PropertyVetoException { long start = System.currentTimeMillis(); try { Connection connection = SimpleDataSourceDemo.getConnection(); long end = System.currentTimeMillis(); System.out.println(end - start); ResultSet resultSet = connection.prepareStatement("select * from testtb").executeQuery(); while (resultSet.next()){ System.out.println(resultSet.getInt("id")+","+resultSet.getString(2)+","+resultSet.getString(3)); } connection.close(); start = System.currentTimeMillis(); connection = SimpleDataSourceDemo.getConnection(); end = System.currentTimeMillis(); System.out.println(end - start); start = System.currentTimeMillis(); resultSet = connection.prepareStatement("select * from testtb").executeQuery(); end = System.currentTimeMillis(); System.out.println(end - start); while (resultSet.next()){ System.out.println(resultSet.getInt("id")+","+resultSet.getString(2)+","+resultSet.getString(3)); } } catch (SQLException e) { e.printStackTrace(); } }}
public class MyConnectionCustomizer implements ConnectionCustomizer { public void onAcquire(Connection c, String parentDataSourceIdentityToken) throws Exception { System.out.println("acquire is invoke ================================="+parentDataSourceIdentityToken); } public void onDestroy(Connection c, String parentDataSourceIdentityToken) throws Exception { System.out.println("destory is invoke ================================="+parentDataSourceIdentityToken); } public void onCheckOut(Connection c, String parentDataSourceIdentityToken) throws Exception { System.out.println("checkout is invoke ================================="+parentDataSourceIdentityToken); } public void onCheckIn(Connection c, String parentDataSourceIdentityToken) throws Exception { System.out.println("checkIn is invoke ================================="+parentDataSourceIdentityToken); }}