Skip to content

Commit

Permalink
bug fixed for "timeBetweenEvictionRunsMillis > keepAliveBetweenTimeMi…
Browse files Browse the repository at this point in the history
…llis"
  • Loading branch information
wenshao committed Apr 19, 2021
1 parent 1d5ae79 commit 71a1b40
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
Expand Up @@ -790,6 +790,10 @@ public void setKeepAliveBetweenTimeMillis(long keepAliveBetweenTimeMillis) {
LOG.error("keepAliveBetweenTimeMillis should be greater than 30000");
}

if (keepAliveBetweenTimeMillis <= timeBetweenEvictionRunsMillis) {
LOG.warn("keepAliveBetweenTimeMillis should be greater than timeBetweenEvictionRunsMillis");
}

this.keepAliveBetweenTimeMillis = keepAliveBetweenTimeMillis;
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/alibaba/druid/pool/DruidDataSource.java
Expand Up @@ -870,6 +870,10 @@ public void init() throws SQLException {
throw new SQLException("maxEvictableIdleTimeMillis must be grater than minEvictableIdleTimeMillis");
}

if (keepAlive && keepAliveBetweenTimeMillis <= timeBetweenEvictionRunsMillis) {
throw new SQLException("keepAliveBetweenTimeMillis must be grater than timeBetweenEvictionRunsMillis");
}

if (this.driverClass != null) {
this.driverClass = driverClass.trim();
}
Expand Down Expand Up @@ -2864,7 +2868,7 @@ public void run() {
for (;;) {
// 从前面开始删除
try {
if (closed) {
if (closed || closing) {
break;
}

Expand Down
Expand Up @@ -165,7 +165,9 @@ public int getIdleConnectionTestPeriod() {
}

public void setIdleConnectionTestPeriod(int idleConnectionTestPeriod) {
dataSource.setTimeBetweenEvictionRunsMillis(((long) idleConnectionTestPeriod) * 1000L);
long timeBetweenEvictionRunsMillis = ((long) idleConnectionTestPeriod) * 1000L;
dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
dataSource.setKeepAliveBetweenTimeMillis(timeBetweenEvictionRunsMillis * 2);
}

public int getInitialPoolSize() {
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/alibaba/druid/bvt/pool/KeepAliveTest.java
Expand Up @@ -37,9 +37,12 @@ protected void tearDown() throws Exception {
public void test_keepAlive() throws Exception {
dataSource.init();

for (int i = 0; i < 1000; ++i) {
if (dataSource.getMinIdle() == dataSource.getPoolingCount()) {
for (int i = 0; i < 100; ++i) {
int poolingCount = dataSource.getPoolingCount();
if (poolingCount >= dataSource.getMinIdle()) {
break;
} else {
System.out.println("poolingCount : " + poolingCount);
}
Thread.sleep(10 * 1);
}
Expand All @@ -55,8 +58,5 @@ public void test_keepAlive() throws Exception {
}
// assertEquals(dataSource.getMaxActive(), dataSource.getPoolingCount());



Thread.sleep(1000 * 1000);
}
}

0 comments on commit 71a1b40

Please sign in to comment.