结论
1.在单线程,进行正常查询的情况下,druid的性能是最好的,Tomcat jdbc其次。
2.在多线程,只获取连接不进行查询的情况下,Tomcat jdbc最好,C3P0其次。
3.在多线程,进行正常查询的情况下,Tomcat jdbc略占优势,因为mysql是部署在服务器上的,进行查询时可能会受到网络波动的影响,所以进行了很多次实验,Tomcat用时少的时候占多数情况。
代码,,是从http://www.cnblogs.com/barrywxx/p/6343303.html这个地方抄的,,然后小改了一下。
package jdbcPoolTest;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import org.apache.tomcat.jdbc.pool.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mysql.jdbc.Connection;
import co.paralleluniverse.fibers.*;
public class Test {
static String driver = "com.mysql.jdbc.Driver";
// 数据库连接地址
static String jdbcUrl = "jdbc:mysql://172.16.12.252:3306/iot_yy?useSSL=false";
// 数据库用户名
static String user = "root";
// 数据库密码
static String passwd = "root";
// 连接池初始化大小
static int initialSize = 5;
// 连接池最小空闲
static int minPoolSize = 10;
// 连接池最大连接数量
static int maxPoolSize = 500;
// 最小逐出时间,100秒
static int maxIdleTime = 100000;
// 连接失败重试次数
static int retryAttempts = 10;
// 当连接池连接耗尽时获取连接数
static int acquireIncrement = 5;
// c3p0数据源
static ComboPooledDataSource c3p0DataSource = getC3p0DataSource();
// Druid数据源
static DruidDataSource druidDataSource = getDruidDataSource();
// Tomcat Jdbc Pool数据源
static DataSource tomcatDataSource = getTomcatDataSource();
static Vector vc=new Vector();
/**
* 测试方式: 每种数据源配置信息尽量相同,以求结果更加准确
* 每种数据源做10次、100次、500次、1000次、2000次、4000次、8000次查询操作 每种查询重复100次,查看100次执行时间的波动图
* @param args
* @throws IOException
* @throws SQLException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, SQLException, InterruptedException {
TestDAO testDAO = new TestDAO();
// 查询次数
int count = 100;
System.out.println("查询次数为:" + count);
System.out.println();
System.out.println("==========================c3p0 测试开始==========================");
// 测试c3p0
for (int i = 0; i < 10; i++) {
queryC3p0(testDAO, c3p0DataSource, count);
}
int sum=0;
for(int j=0;j<10;j++) {
sum+=vc.get(j);
}
System.out.println("sum time "+sum);
System.out.println("avg time "+sum/10);
sum=0;
vc.clear();
System.out.println("==========================c3p0 测试结束==========================");
System.out.println();
System.out.println();
System.out.println("==========================Druid 测试开始==========================");
// 测试Druid
for (int i = 0; i < 10; i++) {
queryDruid(testDAO, druidDataSource, count);
}
for(int j=0;j<10;j++) {
sum+=vc.get(j);
}
System.out.println("sum time "+sum);
System.out.println("avg time "+sum/10);
sum=0;
vc.clear();
System.out.println("==========================Druid 测试结束==========================");
System.out.println();
System.out.println("==========================Tomcat Jdbc Pool 测试开始==========================");
// 测试Tomcat Jdbc Pool
for (int i = 0; i < 10; i++) {
queryTomcatJDBC(testDAO, tomcatDataSource, count);
}
for(int j=0;j<10;j++) {
sum+=vc.get(j);
}
System.out.println("sum time "+sum);
System.out.println("avg time "+sum/10);
sum=0;
vc.clear();
System.out.println("==========================Tomcat Jdbc Pool 测试结束==========================");
//single thread test
// singleLine(testDAO,count);
// long start= System.currentTimeMillis();
// mThread(testDAO,2000, 100);
// long end=System.currentTimeMillis();
// System.out.println("total time "+(end -start));
}
public static void singleLine(TestDAO testDAO, int count) {
try {
queryDruid(testDAO, druidDataSource, count);
queryTomcatJDBC(testDAO, tomcatDataSource, count);
queryC3p0(testDAO, c3p0DataSource, count);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void mThread(TestDAO testDAO,int testCount, int threadCount) throws InterruptedException {
for(int i=0;inew Fiber() {
@Override
protected Object run() throws SuspendExecution, InterruptedException {
// TODO Auto-generated method stub
try {
// queryDruid(testDAO, druidDataSource, testCount);
// queryTomcatJDBC(testDAO, tomcatDataSource, testCount);
queryC3p0(testDAO, c3p0DataSource, testCount);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 1;
}
}.start();
}
while(vc.size()100);
}
int sum=0;
for(int j=0;j"sum time "+sum);
System.out.println("avg time "+sum/threadCount);
}
/**
* c3p0测试
* @param testDAO
* @param ds
* @param count
* @throws SQLException
*/
public static void queryC3p0(TestDAO testDAO, ComboPooledDataSource ds, int count) throws SQLException {
// 查询100次以初始化连接池
for (int i = 0; i < 100; i++) {
testDAO.query(ds.getConnection());
}
// 开始时间
long startMillis = System.currentTimeMillis();
// 循环查询
for (int i = 0; i < count; i++) {
testDAO.query(ds.getConnection());
}
// 结束时间
long endMillis = System.currentTimeMillis();
// 输出结束时间
// System.out.println(endMillis - startMillis);
vc.add(Integer.parseInt((endMillis - startMillis)+""));
}
/**
* Druid测试
* @param testDAO
* @param ds
* @param count
* @throws SQLException
*/
public static void queryDruid(TestDAO testDAO, DruidDataSource ds, int count) throws SQLException {
// 查询100次以初始化连接池
for (int i = 0; i < 100; i++) {
testDAO.query(ds.getConnection());
}
// 开始时间
long startMillis = System.currentTimeMillis();
// 循环查询
for (int i = 0; i < count; i++) {
testDAO.query(ds.getConnection());
}
// 结束时间
long endMillis = System.currentTimeMillis();
// 输出结束时间
// System.out.println(endMillis - startMillis);
vc.add(Integer.parseInt((endMillis - startMillis)+""));
}
/**
* Tomcat Jdbc Pool测试
* @param testDAO
* @param ds
* @param count
* @throws SQLException
*/
public static void queryTomcatJDBC(TestDAO testDAO, DataSource ds, int count) throws SQLException {
// 查询100次以初始化连接池
for (int i = 0; i < 100; i++) {
testDAO.query(ds.getConnection());
}
// 开始时间
long startMillis = System.currentTimeMillis();
// 循环查询
for (int i = 0; i < count; i++) {
testDAO.query(ds.getConnection());
}
// 结束时间
long endMillis = System.currentTimeMillis();
// 输出结束时间
// System.out.println(endMillis - startMillis);
vc.add(Integer.parseInt((endMillis - startMillis)+""));
}
/**
* 获取c3p0数据源
* @throws PropertyVetoException
*/
public static ComboPooledDataSource getC3p0DataSource() {
// 设置参数
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass(driver);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
cpds.setJdbcUrl(jdbcUrl);
cpds.setUser(user);
cpds.setPassword(passwd);
cpds.setInitialPoolSize(initialSize);
cpds.setMinPoolSize(minPoolSize);
cpds.setMaxPoolSize(maxPoolSize);
cpds.setMaxIdleTime(maxIdleTime);
cpds.setAcquireRetryAttempts(retryAttempts);
cpds.setAcquireIncrement(acquireIncrement);
cpds.setTestConnectionOnCheckin(false);
cpds.setTestConnectionOnCheckout(false);
return cpds;
}
/**
* 获取Druid数据源
* @return
*/
public static DruidDataSource getDruidDataSource() {
DruidDataSource dds = new DruidDataSource();
dds.setUsername(user);
dds.setUrl(jdbcUrl);
dds.setPassword(passwd);
dds.setDriverClassName(driver);
dds.setInitialSize(initialSize);
dds.setMaxActive(maxPoolSize);
dds.setMaxWait(maxIdleTime);
dds.setTestWhileIdle(false);
dds.setTestOnReturn(false);
dds.setTestOnBorrow(false);
return dds;
}
/**
* 获取Apache tomcat jdbc pool数据源
* @return
*/
public static DataSource getTomcatDataSource() {
DataSource ds = new DataSource();
ds.setUrl(jdbcUrl);
ds.setUsername(user);
ds.setPassword(passwd);
ds.setDriverClassName(driver);
ds.setInitialSize(initialSize);
ds.setMaxIdle(minPoolSize);
ds.setMaxActive(maxPoolSize);
ds.setTestWhileIdle(false);
ds.setTestOnBorrow(false);
ds.setTestOnConnect(false);
ds.setTestOnReturn(false);
return ds;
}
}