mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor activeConnections record
This commit is contained in:
@@ -680,9 +680,13 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
if (isRemoveAbandoned()) {
|
||||
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
||||
activeConnections.put(poolalbeConnection, new ActiveConnectionTraceInfo(System.currentTimeMillis(),
|
||||
stackTrace));
|
||||
poolalbeConnection.setConnectStackTrace(stackTrace);
|
||||
poolalbeConnection.setConnectedTimeNano();
|
||||
poolalbeConnection.setTraceEnable(true);
|
||||
|
||||
synchronized (activeConnections) {
|
||||
activeConnections.put(poolalbeConnection, PRESENT);
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.isDefaultAutoCommit()) {
|
||||
@@ -791,7 +795,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
// exceptionSorter.isExceptionFatal
|
||||
if (exceptionSorter != null && exceptionSorter.isExceptionFatal(sqlEx)) {
|
||||
if (pooledConnection.isTraceEnable()) {
|
||||
activeConnections.remove(pooledConnection);
|
||||
synchronized (activeConnections) {
|
||||
activeConnections.remove(pooledConnection);
|
||||
}
|
||||
}
|
||||
this.discardConnection(holder.getConnection());
|
||||
pooledConnection.disable();
|
||||
@@ -817,10 +823,12 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
final Connection physicalConnection = holder.getConnection();
|
||||
|
||||
if (pooledConnection.isTraceEnable()) {
|
||||
ActiveConnectionTraceInfo oldInfo = activeConnections.remove(pooledConnection);
|
||||
if (oldInfo == null) {
|
||||
if (LOG.isWarnEnabled()) {
|
||||
LOG.warn("remove abandonded failed. activeConnections.size " + activeConnections.size());
|
||||
synchronized (activeConnections) {
|
||||
Object oldInfo = activeConnections.remove(pooledConnection);
|
||||
if (oldInfo == null) {
|
||||
if (LOG.isWarnEnabled()) {
|
||||
LOG.warn("remove abandonded failed. activeConnections.size " + activeConnections.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1294,35 +1302,41 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
public int removeAbandoned() {
|
||||
int removeCount = 0;
|
||||
|
||||
Iterator<Map.Entry<DruidPooledConnection, ActiveConnectionTraceInfo>> iter = activeConnections.entrySet().iterator();
|
||||
|
||||
long currentMillis = System.currentTimeMillis();
|
||||
long currrentNanos = System.nanoTime();
|
||||
|
||||
List<DruidPooledConnection> abandonedList = new ArrayList<DruidPooledConnection>();
|
||||
|
||||
for (; iter.hasNext();) {
|
||||
Map.Entry<DruidPooledConnection, ActiveConnectionTraceInfo> entry = iter.next();
|
||||
DruidPooledConnection pooledConnection = entry.getKey();
|
||||
synchronized (activeConnections) {
|
||||
Iterator<DruidPooledConnection> iter = activeConnections.keySet().iterator();
|
||||
|
||||
if (pooledConnection.isRunning()) {
|
||||
continue;
|
||||
for (; iter.hasNext();) {
|
||||
DruidPooledConnection pooledConnection = iter.next();
|
||||
|
||||
if (pooledConnection.isRunning()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
long timeMillis = (currrentNanos - pooledConnection.getConnectedTimeNano()) / (1000 * 1000);
|
||||
|
||||
if (timeMillis >= removeAbandonedTimeoutMillis) {
|
||||
iter.remove();
|
||||
abandonedList.add(pooledConnection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ActiveConnectionTraceInfo activeInfo = entry.getValue();
|
||||
long timeMillis = currentMillis - activeInfo.getConnectTime();
|
||||
|
||||
if (timeMillis >= removeAbandonedTimeoutMillis) {
|
||||
if (abandonedList.size() > 0) {
|
||||
for (DruidPooledConnection pooledConnection : abandonedList) {
|
||||
JdbcUtils.close(pooledConnection);
|
||||
pooledConnection.abandond();
|
||||
removeAbandonedCount++;
|
||||
removeCount++;
|
||||
abandonedList.add(pooledConnection);
|
||||
|
||||
if (isLogAbandoned()) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("abandon connection, open stackTrace\n");
|
||||
|
||||
StackTraceElement[] trace = activeInfo.getStackTrace();
|
||||
StackTraceElement[] trace = pooledConnection.getConnectStackTrace();
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
buf.append("\tat ");
|
||||
buf.append(trace[i].toString());
|
||||
@@ -1334,11 +1348,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
|
||||
// multi-check dup close
|
||||
for (DruidPooledConnection conn : abandonedList) {
|
||||
activeConnections.remove(conn);
|
||||
}
|
||||
|
||||
return removeCount;
|
||||
}
|
||||
|
||||
@@ -1353,26 +1362,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
return ref;
|
||||
}
|
||||
|
||||
static class ActiveConnectionTraceInfo {
|
||||
|
||||
private final long connectTime;
|
||||
private final StackTraceElement[] stackTrace;
|
||||
|
||||
public ActiveConnectionTraceInfo(long connectTime, StackTraceElement[] stackTrace){
|
||||
super();
|
||||
this.connectTime = connectTime;
|
||||
this.stackTrace = stackTrace;
|
||||
}
|
||||
|
||||
public long getConnectTime() {
|
||||
return connectTime;
|
||||
}
|
||||
|
||||
public StackTraceElement[] getStackTrace() {
|
||||
return stackTrace;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getFilterClassNames() {
|
||||
List<String> names = new ArrayList<String>();
|
||||
|
||||
Reference in New Issue
Block a user