作者:1157476701qq | 来源:互联网 | 2023-02-08 15:15
这次所要做的测试是比较几种我们常用的数据库连接池的性能,他们分别是:c3p0,Proxool,Druid,TomcatJdbcPool这四种,测试将采用统一的参数配置力求比较“公平”的体现统
这次所要做的测试是比较几种我们常用的数据库连接池的性能,他们分别是:c3p0 ,Proxool ,Druid ,Tomcat Jdbc Pool这四种,测试将采用统一的参数配置力求比较“公平”的体现统一水平下几种连接池的不同,有网友回复说测试不公平会互相干扰,那我就把代码分开,代码是死的人是活的,做事情不动脑只能吃别人剩下的,世界上没有绝对公平的事情,我在此只提供了一个思路,更多的测试还需要你自己去完成。
1.创建类TestDAO,封装统一的查询方法 :
Java代码
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
-
- public class TestDAO {
-
- private final static String sql = "SELECT * FROM USER u WHERE u.USR_ID=9999";
-
- public void query(Connection conn) {
- try {
- Statement st = conn.createStatement();
- ResultSet result = st.executeQuery(sql);
- result.close();
- st.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
2.创建测试类TestMain,其中包含:统一参数配置属性、获取各种连接池数据源方法、各种数据源调用方法等,下面分别是各种数据源的测试Main方法,开始测试之前做100次查询操作以初始化连接池并起到稳定测试结果作用:
(1)c3p0测试代码:
Java代码
- import java.beans.PropertyVetoException;
- import java.io.IOException;
- import java.sql.SQLException;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
-
- public class C3p0Test {
-
-
- final static String driver = "com.mysql.jdbc.Driver";
-
- final static String jdbcUrl = "jdbc:mysql://192.168.0.1:3306/test";
-
- final static String user = "dba";
-
- final static String passwd = "dba";
-
- final static int initialSize = 5;
-
- final static int minPoolSize = 10;
-
- final static int maxPoolSize = 50;
-
- final static int maxIdleTime = 100000;
-
- final static int retryAttempts = 10;
-
- final static int acquireIncrement = 5;
-
- final static ComboPooledDataSource c3p0DataSource = getC3p0DataSource();
-
-
- final static int count = 10;
-
-
-
-
-
-
-
-
- public static void main(String[] args) throws IOException, SQLException {
-
- TestDAO testDAO = new TestDAO();
- System.out.println("查询次数为:" + count);
- System.out.println();
- System.out.println("==========================c3p0 测试开始==========================");
-
- for (int i = 0; i < 100; i++) {
- queryC3p0(testDAO, c3p0DataSource, count);
- }
- System.out.println("==========================c3p0 测试结束==========================");
- }
-
-
-
-
-
-
-
-
- public static void queryC3p0(TestDAO testDAO, ComboPooledDataSource ds, int count) throws SQLException {
-
- 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);
- }
-
-
-
-
-
- public static ComboPooledDataSource getC3p0DataSource() {
-
- ComboPooledDataSource cpds = new ComboPooledDataSource();
- try {
- cp