Skip to content

Commit 71a1b40

Browse files
committed
bug fixed for "timeBetweenEvictionRunsMillis > keepAliveBetweenTimeMillis"
1 parent 1d5ae79 commit 71a1b40

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,10 @@ public void setKeepAliveBetweenTimeMillis(long keepAliveBetweenTimeMillis) {
790790
LOG.error("keepAliveBetweenTimeMillis should be greater than 30000");
791791
}
792792

793+
if (keepAliveBetweenTimeMillis <= timeBetweenEvictionRunsMillis) {
794+
LOG.warn("keepAliveBetweenTimeMillis should be greater than timeBetweenEvictionRunsMillis");
795+
}
796+
793797
this.keepAliveBetweenTimeMillis = keepAliveBetweenTimeMillis;
794798
}
795799

src/main/java/com/alibaba/druid/pool/DruidDataSource.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,10 @@ public void init() throws SQLException {
870870
throw new SQLException("maxEvictableIdleTimeMillis must be grater than minEvictableIdleTimeMillis");
871871
}
872872

873+
if (keepAlive && keepAliveBetweenTimeMillis <= timeBetweenEvictionRunsMillis) {
874+
throw new SQLException("keepAliveBetweenTimeMillis must be grater than timeBetweenEvictionRunsMillis");
875+
}
876+
873877
if (this.driverClass != null) {
874878
this.driverClass = driverClass.trim();
875879
}
@@ -2864,7 +2868,7 @@ public void run() {
28642868
for (;;) {
28652869
// 从前面开始删除
28662870
try {
2867-
if (closed) {
2871+
if (closed || closing) {
28682872
break;
28692873
}
28702874

src/main/java/com/alibaba/druid/pool/DruidDataSourceC3P0Adapter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ public int getIdleConnectionTestPeriod() {
165165
}
166166

167167
public void setIdleConnectionTestPeriod(int idleConnectionTestPeriod) {
168-
dataSource.setTimeBetweenEvictionRunsMillis(((long) idleConnectionTestPeriod) * 1000L);
168+
long timeBetweenEvictionRunsMillis = ((long) idleConnectionTestPeriod) * 1000L;
169+
dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
170+
dataSource.setKeepAliveBetweenTimeMillis(timeBetweenEvictionRunsMillis * 2);
169171
}
170172

171173
public int getInitialPoolSize() {

src/test/java/com/alibaba/druid/bvt/pool/KeepAliveTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ protected void tearDown() throws Exception {
3737
public void test_keepAlive() throws Exception {
3838
dataSource.init();
3939

40-
for (int i = 0; i < 1000; ++i) {
41-
if (dataSource.getMinIdle() == dataSource.getPoolingCount()) {
40+
for (int i = 0; i < 100; ++i) {
41+
int poolingCount = dataSource.getPoolingCount();
42+
if (poolingCount >= dataSource.getMinIdle()) {
4243
break;
44+
} else {
45+
System.out.println("poolingCount : " + poolingCount);
4346
}
4447
Thread.sleep(10 * 1);
4548
}
@@ -55,8 +58,5 @@ public void test_keepAlive() throws Exception {
5558
}
5659
// assertEquals(dataSource.getMaxActive(), dataSource.getPoolingCount());
5760

58-
59-
60-
Thread.sleep(1000 * 1000);
6161
}
6262
}

0 commit comments

Comments
 (0)