您的位置:首页 > 编程语言

自己测试的c3p0的代码demo

2016-06-14 20:25 239 查看
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: