mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
fixed removedAbandond problem
This commit is contained in:
@@ -1243,7 +1243,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
long currentMillis = System.currentTimeMillis();
|
||||
|
||||
List<DruidPooledConnection> abondonedList = new ArrayList<DruidPooledConnection>();
|
||||
List<DruidPooledConnection> abandonedList = new ArrayList<DruidPooledConnection>();
|
||||
|
||||
for (; iter.hasNext();) {
|
||||
Map.Entry<DruidPooledConnection, ActiveConnectionTraceInfo> entry = iter.next();
|
||||
@@ -1257,10 +1257,11 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
long timeMillis = currentMillis - activeInfo.getConnectTime();
|
||||
|
||||
if (timeMillis >= removeAbandonedTimeoutMillis) {
|
||||
pooledConnection.abandond();
|
||||
JdbcUtils.close(pooledConnection);
|
||||
removeAbandonedCount++;
|
||||
removeCount++;
|
||||
abondonedList.add(pooledConnection);
|
||||
abandonedList.add(pooledConnection);
|
||||
|
||||
if (isLogAbandoned()) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
@@ -1279,7 +1280,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
// multi-check dup close
|
||||
for (DruidPooledConnection conn : abondonedList) {
|
||||
for (DruidPooledConnection conn : abandonedList) {
|
||||
activeConnections.remove(conn);
|
||||
}
|
||||
|
||||
|
||||
@@ -61,13 +61,15 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
protected TransactionInfo transactionInfo;
|
||||
private final boolean dupCloseLogEnable;
|
||||
private boolean traceEnable = false;
|
||||
private boolean disable = false;
|
||||
private boolean disable = false;
|
||||
private boolean closed = false;
|
||||
private final Thread ownerThread;
|
||||
|
||||
private long connectedTimeNano;
|
||||
private volatile boolean running = false;
|
||||
|
||||
private volatile boolean abandoned = false;
|
||||
|
||||
public DruidPooledConnection(DruidConnectionHolder holder){
|
||||
this.conn = holder.getConnection();
|
||||
this.holder = holder;
|
||||
@@ -190,7 +192,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
} else {
|
||||
recycle();
|
||||
}
|
||||
|
||||
|
||||
this.disable = true;
|
||||
}
|
||||
|
||||
@@ -198,7 +200,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
if (this.disable) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
DruidConnectionHolder holder = this.holder;
|
||||
if (holder == null) {
|
||||
if (dupCloseLogEnable) {
|
||||
@@ -207,7 +209,9 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
return;
|
||||
}
|
||||
|
||||
holder.getDataSource().recycle(this);
|
||||
if (!this.abandoned) {
|
||||
holder.getDataSource().recycle(this);
|
||||
}
|
||||
|
||||
this.holder = null;
|
||||
conn = null;
|
||||
@@ -759,6 +763,10 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
|
||||
return conn.isClosed();
|
||||
}
|
||||
|
||||
public boolean isAbandonded() {
|
||||
return this.abandoned;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DatabaseMetaData getMetaData() throws SQLException {
|
||||
@@ -1024,4 +1032,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
return running;
|
||||
}
|
||||
|
||||
public void abandond() {
|
||||
this.abandoned = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,12 +15,11 @@
|
||||
*/
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.stat.DruidDataSourceStatManager;
|
||||
|
||||
public class TestAbondon extends TestCase {
|
||||
@@ -46,9 +45,9 @@ public class TestAbondon extends TestCase {
|
||||
}
|
||||
|
||||
public void test_0() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
DruidPooledConnection conn = dataSource.getConnection();
|
||||
Assert.assertEquals(false, conn.isClosed());
|
||||
Thread.sleep(200);
|
||||
Assert.assertEquals(true, conn.isClosed());
|
||||
Assert.assertEquals(true, conn.isAbandonded());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user