当前位置: 代码迷 >> J2EE >> C3P0跟jdbc 多用户访问哪个更好点
  详细解决方案

C3P0跟jdbc 多用户访问哪个更好点

热度:291   发布时间:2016-04-21 21:36:53.0
C3P0和jdbc 多用户访问哪个更好点?
最近在考虑数据库设计,多用户访问,想采用数据库连接池。
但对比c3po和jdbc,感觉性能提升上不是很明显,不知道是不是我测试的有问题?求解?

package com.yongan.pool;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class TestPool {

private static ComboPooledDataSource datasource;
private static TestPool instance;

/*
 * 初始化数据库连接
 */
private TestPool(){
datasource = new ComboPooledDataSource();
try {
datasource.setUser("root");
datasource.setPassword("123456");
datasource.setJdbcUrl("jdbc:mysql://localhost/traffic");
datasource.setDriverClass("org.gjt.mm.mysql.Driver");
datasource.setInitialPoolSize(5);
datasource.setMinPoolSize(1);
datasource.setMaxPoolSize(10);
datasource.setMaxIdleTime(60);
datasource.setMaxStatements(50);
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static TestPool getManagerConnection(){
if(instance == null){
instance = new TestPool();
}
return instance;
}


public static Connection getConnection(){
Connection con = null;
try {
con = datasource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;

}

public static Connection getNotPoolConnection(){
Connection con = null;
try {
Class.forName("org.gjt.mm.mysql.Driver");
String url = "jdbc:mysql://localhost/traffic";
String user = "root";
String password = "123456";
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {

System.out.println("不使用线程池");
for(int i=0 ; i< 10 ; i++){
long beginTime = System.currentTimeMillis();
Connection con = TestPool.getNotPoolConnection();
try{
PreparedStatement   pst = con.prepareStatement("select * from tb_action");
ResultSet rs = pst.executeQuery();
while(rs.next()){

}
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
System.out.println("第 " + i +" 次 ,总共用了 "+(endTime-beginTime)+" ms");
}

try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("----------------------------------------");

System.out.println("使用线程池");
for(int i=0 ; i< 10 ; i++){
long beginTime = System.currentTimeMillis();
Connection con = TestPool.getManagerConnection().getConnection();
try{
PreparedStatement   pst = con.prepareStatement("select * from tb_action");
ResultSet rs = pst.executeQuery();
while(rs.next()){

}
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
System.out.println("第 " + i +" 次 ,总共用了 "+(endTime-beginTime)+" ms");
}

}

}



结果

不使用线程池
第 0 次 ,总共用了 150 ms
第 1 次 ,总共用了 10 ms
第 2 次 ,总共用了 10 ms
第 3 次 ,总共用了 10 ms
第 4 次 ,总共用了 0 ms
第 5 次 ,总共用了 10 ms
第 6 次 ,总共用了 10 ms
第 7 次 ,总共用了 0 ms
第 8 次 ,总共用了 10 ms
第 9 次 ,总共用了 0 ms
----------------------------------------
使用线程池
2013-11-21 14:00:56 com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
2013-11-21 14:00:56 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.2 [built 09-February-2013 02:13:17 +0000; debug? true; trace: 10]
  相关解决方案