mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fix for keepAlive, fix #4316
This commit is contained in:
@@ -2528,10 +2528,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
return false;
|
||||
}
|
||||
|
||||
return put(holder, physicalConnectionInfo.createTaskId);
|
||||
return put(holder, physicalConnectionInfo.createTaskId, false);
|
||||
}
|
||||
|
||||
private boolean put(DruidConnectionHolder holder, long createTaskId) {
|
||||
private boolean put(DruidConnectionHolder holder, long createTaskId, boolean checkExists) {
|
||||
lock.lock();
|
||||
try {
|
||||
if (this.closing || this.closed) {
|
||||
@@ -2544,6 +2544,15 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (checkExists) {
|
||||
for (int i = 0; i < poolingCount; i++) {
|
||||
if (connections[i] == holder) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
connections[poolingCount] = holder;
|
||||
incrementPoolingCount();
|
||||
|
||||
@@ -3207,7 +3216,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
boolean discard = !validate;
|
||||
if (validate) {
|
||||
holer.lastKeepTimeMillis = System.currentTimeMillis();
|
||||
boolean putOk = put(holer, 0L);
|
||||
boolean putOk = put(holer, 0L, true);
|
||||
if (!putOk) {
|
||||
discard = true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class Issue4316 {
|
||||
@Test
|
||||
public void test_0() throws Exception {
|
||||
DruidDataSource druidDataSource = new DruidDataSource();
|
||||
druidDataSource.setUsername("");
|
||||
druidDataSource.setPassword("");
|
||||
druidDataSource.setUrl("jdbc:mock:xxx");
|
||||
druidDataSource.setValidationQuery("select 1");
|
||||
druidDataSource.setMinEvictableIdleTimeMillis(100000);
|
||||
druidDataSource.setMaxEvictableIdleTimeMillis(DruidDataSource.DEFAULT_MAX_EVICTABLE_IDLE_TIME_MILLIS);
|
||||
druidDataSource.setKeepAliveBetweenTimeMillis(120000);
|
||||
druidDataSource.setMinIdle(2);
|
||||
druidDataSource.setTimeBetweenEvictionRunsMillis(70000);
|
||||
druidDataSource.setKeepAlive(true);
|
||||
DruidPooledConnection connection1 = druidDataSource.getConnection();
|
||||
DruidPooledConnection connection2 = druidDataSource.getConnection();
|
||||
connection2.close();
|
||||
Thread.sleep(95000);
|
||||
connection1.close();
|
||||
Thread.sleep(140000);
|
||||
DruidPooledConnection connection3 = druidDataSource.getConnection();
|
||||
DruidPooledConnection connection4 = druidDataSource.getConnection();
|
||||
assertFalse(connection3.getConnectionHolder() == connection4.getConnectionHolder());
|
||||
connection3.close();
|
||||
connection4.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user