From fe8b5b292f1b9cafa83498b10bc8fc6002f6b8b5 Mon Sep 17 00:00:00 2001 From: wenshao Date: Fri, 9 Sep 2011 17:00:21 +0000 Subject: [PATCH] bug fixed git-svn-id: http://code.alibabatech.com/svn/druid/trunk@708 b9813039-fb51-4c41-a8b9-e21c2acb5095 --- .../alibaba/druid/pool/DruidDataSource.java | 2 +- .../druid/bvt/pool/TestRemoveAbandoned.java | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/TestRemoveAbandoned.java diff --git a/src/main/java/com/alibaba/druid/pool/DruidDataSource.java b/src/main/java/com/alibaba/druid/pool/DruidDataSource.java index 946d16d7a3..3c3d1a8394 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/DruidDataSource.java @@ -838,7 +838,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat for (; iter.hasNext();) { Map.Entry entry = iter.next(); ActiveConnectionTraceInfo activeInfo = entry.getValue(); - long timeMillis = activeInfo.getConnectTime() - currentMillis; + long timeMillis = currentMillis - activeInfo.getConnectTime(); if (timeMillis >= removeAbandonedTimeoutMillis) { PoolableConnection pooledConnection = entry.getKey(); diff --git a/src/test/java/com/alibaba/druid/bvt/pool/TestRemoveAbandoned.java b/src/test/java/com/alibaba/druid/bvt/pool/TestRemoveAbandoned.java new file mode 100644 index 0000000000..43a1cecbbe --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/TestRemoveAbandoned.java @@ -0,0 +1,50 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.Connection; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.mock.MockDriver; +import com.alibaba.druid.pool.DruidDataSource; + + +public class TestRemoveAbandoned extends TestCase { + private MockDriver driver; + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + driver = new MockDriver(); + + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setDriver(driver); + dataSource.setInitialSize(1); + dataSource.setMaxActive(2); + dataSource.setMaxIdle(2); + dataSource.setMinIdle(1); + dataSource.setMinEvictableIdleTimeMillis(300 * 1000); // 300 / 10 + dataSource.setTimeBetweenEvictionRunsMillis(10); // 180 / 10 + dataSource.setRemoveAbandoned(true); + dataSource.setRemoveAbandonedTimeoutMillis(1); + dataSource.setTestWhileIdle(true); + dataSource.setTestOnBorrow(false); + dataSource.setValidationQuery("SELECT 1"); + dataSource.setFilters("stat"); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_removeAbandoned() throws Exception { + Connection conn = dataSource.getConnection(); + Assert.assertEquals(1, dataSource.getActiveCount()); + Assert.assertEquals(0, dataSource.getPoolingCount()); + Thread.sleep(100); + Assert.assertTrue(conn.isClosed()); + + Assert.assertEquals(0, dataSource.getActiveCount()); + Assert.assertEquals(1, dataSource.getPoolingCount()); + } +}