首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

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

2013-11-23 
C3P0和jdbc 多用户访问哪个更好点?最近在考虑数据库设计,多用户访问,想采用数据库连接池。但对比c3po和jdbc

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]


2013-11-21 14:00:56 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge14y8yxn3owlocizjd|1b90b39, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> org.gjt.mm.mysql.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge14y8yxn3owlocizjd|1b90b39, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, jdbcUrl -> jdbc:mysql://localhost/traffic, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 60, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 1, numHelperThreads -> 3, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
第 0 次 ,总共用了 205 ms
第 1 次 ,总共用了 0 ms
第 2 次 ,总共用了 0 ms
第 3 次 ,总共用了 0 ms
第 4 次 ,总共用了 0 ms
第 5 次 ,总共用了 0 ms
第 6 次 ,总共用了 0 ms
第 7 次 ,总共用了 0 ms
第 8 次 ,总共用了 0 ms
第 9 次 ,总共用了 0 ms




从结果来看,不用C3PO的话,第一次读取慢点,后面读取也快,有时还有0秒(难道也有连接池的?)
使用C3P0的话,第一次读取慢,后面0读取速度
[解决办法]
你本地怎么能测的出呢··
用jdbc你得自己管理资源
想想都头大
特别是高并发的时候··
特别是忘记写个关闭什么的
分分钟撑爆
[解决办法]
引用:
恩,我想是明白了。如果真要模拟,是否可以开10个线程模拟10个用户?


最简单的做法就是用LoadRunner 做个压力

其实如果说你们开发人的水平相当平均,自己管理jdbc连接池都ok的话,也不是不能用jdbc,但是项目维护的话,c3p0改一个地方就可以,你jdbc要改所有用到的地方不是么。若一个人写jdbc没写好,没及时释放连接,分分钟数据库就没有连接数了,找问题就头疼了。

热点排行