测试发现commons-dbcp2没有缓存Connection对象

由于项目需要用到DataSource,自然而然想到的就是apache的comments-dbcp!因为用的是JDK7,自然而然引用的就是commons-dbcp2-2.1.1.jar。在使用的过程中想到了之前写的一个测试redis.clients.jedis.JedisPool的代码,就把直接拿过来修改一下跑了一下,结果发现Connection对象没有缓存到池中!!!!

首先检测自己的代码,继续查找原因……始终结果一样!

最后想到了换成1.4版本的,测试结果是预期想要的!

现贴出测试代码请各位替我把把关!

public class BasicDataSourceForMysqlTest {
    private static final String url = "jdbc:mysql://127.0.0.1:3306/test";
    private static final String username = "root";
    private static final String password = "";
    private static final String driverClassName = "com.mysql.jdbc.Driver";

    private static org.apache.commons.dbcp2.BasicDataSource dataSource;

    /*dataSource = new BasicDataSource();
        dataSource = new BasicDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setInitialSize(5);
        dataSource.setMaxIdle(5);
        dataSource.setMinIdle(5);
        dataSource.setValidationQuery("select 1;");*/
    static {
        java.util.Properties props = new java.util.Properties();
        props.put("driverClassName", driverClassName);
        props.put("url", url);
        props.put("username", username);
        props.put("password", password);
        props.put("minIdle", 5);
        props.put("mzxIdle", 5);
        props.put("maxActive", 5);
        props.put("initialSize", 5);
        props.put("logAbandoned", true);
        props.put("removeAbandoned", true);
        props.put("maxWait", 500);
        props.put("validationQuery", "select 1;");
        try {
            dataSource = org.apache.commons.dbcp2.BasicDataSourceFactory.createDataSource(props);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        final java.util.Set<String> set = new java.util.HashSet<>();
        int count = 0;
        while (count <= 10) {
            int num = 5;
            final java.util.concurrent.CountDownLatch startSignal = new java.util.concurrent.CountDownLatch(num);
            try {
                Thread.sleep(600);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            for (int i = 0; i < num; i++) {
                new Thread("testThread_" + (i + 1)) {
                    @Override
                    public void run() {
                        try {
                            startSignal.countDown();
                            java.sql.Connection client = dataSource.getConnection();
                            set.add(client.hashCode() + "");
                            try {
                                System.out.println(getName() + "__" + dataSource.getNumActive() + "---" + dataSource.getNumIdle() + "---" + dataSource.getInitialSize());
                                System.out.println(client.hashCode());
                            } finally {
                                client.close();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
            try {
                startSignal.await();
            } catch (InterruptedException e) {
            }
            System.out.println("start " + count);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            count++;
        }
        System.err.println("Over ... " + set.toString() + "_____" + set.size());

        try {
            dataSource.close();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-dbcp2</artifactId>
			<version>2.1.1</version>
		</dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

1.4版本的测试结果,也是预期测试结果!

start 0
testThread_3__2---0---0
testThread_4__2---0---0
223589918
510850432
testThread_5__1---2---0
64319410
testThread_2__1---3---0
131974647
testThread_1__1---4---0
1894768425
testThread_1__2---1---0
1894768425
testThread_3__3---1---0
testThread_2__3---1---0
64319410
start 1
131974647
testThread_4__1---3---0
510850432
testThread_5__1---4---0
223589918
testThread_1__2---2---0
testThread_2__2---2---0
510850432
testThread_4__3---2---0
223589918
131974647
start 2
testThread_5__1---3---0
510850432
testThread_3__1---4---0
131974647
testThread_2__3---1---0
testThread_3__3---1---0
223589918
testThread_1__3---1---0
131974647
start 3
testThread_4__4---1---0
64319410
510850432
testThread_5__1---4---0
223589918
testThread_2__3---1---0
testThread_3__3---1---0
64319410
testThread_1__3---1---0
510850432
223589918
start 4
testThread_4__3---1---0
131974647
testThread_5__2---3---0
1894768425
testThread_2__2---2---0
testThread_1__2---2---0
1894768425
131974647
testThread_3__3---2---0
223589918
start 5
testThread_4__1---3---0
131974647
testThread_5__1---4---0
1894768425
testThread_2__1---1---0
131974647
testThread_1__2---1---0
1894768425
testThread_4__2---2---0
223589918
start 6
testThread_5__1---3---0
131974647
testThread_3__1---4---0
510850432
testThread_2__2---2---0
131974647
testThread_1__2---2---0
510850432
testThread_3__2---3---0
223589918
start 7
testThread_4__2---3---0
510850432
testThread_5__2---3---0
223589918
testThread_1__1---1---0
223589918
testThread_2__2---1---0
1894768425
testThread_4__2---1---0
testThread_3__3---1---0
510850432
start 8
testThread_5__2---3---0
223589918
131974647
testThread_2__2---2---0
testThread_1__2---2---0
131974647
223589918
testThread_3__1---3---0
510850432
start 9
testThread_4__1---3---0
223589918
testThread_5__1---4---0
131974647
testThread_2__2---1---0
223589918
testThread_1__2---1---0
131974647
testThread_3__2---2---0
510850432
start 10
testThread_5__2---2---0
1894768425
testThread_4__1---4---0
64319410
Over ... [1894768425, 64319410, 223589918, 131974647, 510850432]_____5

2.1和2.1.1的测试结果

start 0
testThread_2__4---0---0
1510417198
testThread_3__4---0---0
1664207618
testThread_1__4---0---0
177655599
testThread_4__4---0---0
855383063
testThread_5__1---4---0
624427163
start 1
testThread_1__5---0---0
509459080
testThread_3__5---0---0
461456971
testThread_2__5---0---0
1143862280
testThread_4__2---3---0
1923254538
testThread_5__1---4---0
215165522
testThread_2__3---2---0
1337836835
testThread_1__3---2---0
869901755
testThread_3__4---1---0
370127509
start 2
testThread_4__2---3---0
1620402051
testThread_5__1---4---0
1857143550
testThread_2__4---1---0
1213238765
start 3
testThread_3__4---1---0
1369335764
testThread_1__4---1---0
546695090
testThread_5__3---2---0
1493347140
testThread_4__4---0---0
1991424296
testThread_1__4---1---0
1703954167
start 4
testThread_2__3---2---0
1663252024
testThread_3__3---2---0
492942072
testThread_4__3---2---0
2032977625
testThread_5__1---4---0
1707791737
testThread_2__4---1---0
1235983733
testThread_1__4---1---0
569283100
testThread_3__3---2---0
1279460130
start 5
testThread_4__2---3---0
1641427189
testThread_5__1---4---0
891836161
testThread_1__3---2---0
1637289197
start 6
testThread_3__4---1---0
64081321
testThread_5__4---1---0
431404444
testThread_2__3---2---0
709698036
testThread_4__1---4---0
1076326904
testThread_1__4---1---0
2022316086
testThread_3__4---1---0
842776333
start 7
testThread_4__3---2---0
670897142
testThread_5__2---3---0
279713436
testThread_2__2---3---0
1471810322
testThread_1__4---1---0
1134728957
testThread_3__4---1---0
1734794939
testThread_2__4---1---0
313064454
start 8
testThread_4__3---2---0
1772550211
testThread_5__2---4---0
190755917
testThread_2__4---1---0
testThread_3__4---1---0
1027738753
1256624859
testThread_1__4---1---0
1739820615
start 9
testThread_4__4---1---0
994248850
testThread_5__2---3---0
437312607
testThread_1__3---2---0
348190607
testThread_3__3---2---0
testThread_2__3---1---0
testThread_4__4---1---0
1691184586
287765893
start 10
637816509
testThread_5__1---4---0
2138472547
Over ... [1471810322, 287765893, 1703954167, 2138472547, 1510417198, 624427163, 1739820615, 891836161, 492942072, 64081321, 437312607, 370127509, 1027738753, 1664207618, 709698036, 215165522, 569283100, 1663252024, 1337836835, 279713436, 546695090, 1637289197, 1707791737, 509459080, 1076326904, 1213238765, 1279460130, 177655599, 637816509, 1620402051, 1235983733, 1256624859, 431404444, 348190607, 461456971, 1772550211, 842776333, 994248850, 1991424296, 1734794939, 1641427189, 1143862280, 1923254538, 869901755, 1369335764, 855383063, 1691184586, 1493347140, 313064454, 1134728957, 190755917, 2022316086, 1857143550, 2032977625, 670897142]_____55

 

转载于:https://my.oschina.net/u/1250501/blog/909222

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值