Browse Source

druidDatasource断线重连机制有点问题

release/9.0
abel.chen 6 years ago
parent
commit
16d4dbe6f2
  1. 149
      fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java

149
fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java

@ -15,6 +15,43 @@
*/
package com.fr.third.alibaba.druid.pool;
import java.io.Closeable;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.management.JMException;
import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.StringRefAddr;
import javax.sql.ConnectionEvent;
import javax.sql.ConnectionEventListener;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.PooledConnection;
import com.fr.third.alibaba.druid.Constants;
import com.fr.third.alibaba.druid.TransactionTimeoutException;
import com.fr.third.alibaba.druid.VERSION;
@ -58,41 +95,6 @@ import com.fr.third.alibaba.druid.util.Utils;
import com.fr.third.alibaba.druid.wall.WallFilter;
import com.fr.third.alibaba.druid.wall.WallProviderStatValue;
import javax.management.JMException;
import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.StringRefAddr;
import javax.sql.ConnectionEvent;
import javax.sql.ConnectionEventListener;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.PooledConnection;
import java.io.Closeable;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author ljw [ljw2083@alibaba-inc.com]
* @author wenshao [szujobs@hotmail.com]
@ -142,7 +144,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
private LogStatsThread logStatsThread;
private int createTaskCount;
private final CountDownLatch initedLatch = new CountDownLatch(2);
private CountDownLatch initedLatch = new CountDownLatch(2);
private volatile boolean enable = true;
@ -166,8 +168,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
private volatile boolean keepAlive = false;
private static final boolean logActiveCount = "true".equals(System.getProperty("frDruidLogActiveCount"));
public DruidDataSource() {
this(false);
}
@ -603,6 +603,30 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
this.connectProperties = properties;
}
private synchronized void createThreadChange() {
String threadName = "Druid-ConnectionPool-Create-" + System.identityHashCode(this) + this.getUrl();
createConnectionThread = new CreateConnectionThread(threadName);
createConnectionThread.setStarted(false);
String destroyName = "Druid-ConnectionPool-Destroy-" + System.identityHashCode(this) + this.getUrl();
destroyConnectionThread = new DestroyConnectionThread(destroyName);
destroyConnectionThread.setStarted(false);
initedLatch = new CountDownLatch(2);
}
private void checkThread() throws SQLException {
if (!createConnectionThread.isStarted() && !destroyConnectionThread.isStarted()) {
createConnectionThread.setStarted(true);
createConnectionThread.start();
destroyConnectionThread.setStarted(true);
destroyConnectionThread.start();
try {
initedLatch.await();
} catch (InterruptedException e) {
throw new SQLException(e.getMessage(), e);
}
}
}
public void init() throws SQLException {
if (inited) {
return;
@ -818,7 +842,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
protected void createAndStartCreatorThread() {
if (createScheduler == null) {
String threadName = "Druid-ConnectionPool-Create-" + System.identityHashCode(this);
String threadName = "Druid-ConnectionPool-Create-" + System.identityHashCode(this) + this.getUrl();
createConnectionThread = new CreateConnectionThread(threadName);
createConnectionThread.start();
return;
@ -1056,7 +1080,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
public DruidPooledConnection getConnection(long maxWaitMillis) throws SQLException {
init();
checkThread();
if (filters.size() > 0) {
FilterChainImpl filterChain = new FilterChainImpl(this);
return filterChain.dataSource_connect(this, maxWaitMillis);
@ -1167,7 +1191,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
lock.lock();
try {
activeCount--;
logActiveCount(false);
discardCount++;
if (activeCount <= minIdle) {
@ -1219,7 +1242,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
if (holder != null) {
activeCount++;
logActiveCount(true);
if (activeCount > activePeak) {
activePeak = activeCount;
activePeakTime = System.currentTimeMillis();
@ -1251,8 +1273,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
.append(", active " + activeCount)//
.append(", maxActive " + maxActive)//
;
List<JdbcSqlStatValue> sqlList = this.getDataSourceStat().getRuningSqlList();
JdbcDataSourceStat sourceStat = this.getDataSourceStat();
if (sourceStat != null) {
List<JdbcSqlStatValue> sqlList = sourceStat.getRuningSqlList();
for (int i = 0; i < sqlList.size(); ++i) {
if (i != 0) {
buf.append('\n');
@ -1265,6 +1288,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
buf.append(" : ");
buf.append(sql.getSql());
}
}
String errorMessage = buf.toString();
throw new GetConnectionTimeoutException(errorMessage);
}
@ -1409,7 +1433,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
lock.lock();
try {
activeCount--;
logActiveCount(false);
closeCount++;
} finally {
lock.unlock();
@ -1428,7 +1451,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
lock.lockInterruptibly();
try {
activeCount--;
logActiveCount(false);
closeCount++;
result = putLast(holder, lastActiveTimeMillis);
@ -2017,9 +2039,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
LOG.error("create connection error", e);
// unknow fatal exception
setFailContinuous(true);
if (breakAfterAcquireFailure) {
break;
}
continue;
} catch (Error e) {
lock.lock();
@ -2049,6 +2068,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
public class CreateConnectionThread extends Thread {
private volatile boolean started = true;
public CreateConnectionThread(String name) {
super(name);
@ -2132,13 +2152,12 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
} catch (InterruptedException interruptEx) {
break;
}
DruidDataSource.this.createThreadChange();
break;
}
} catch (RuntimeException e) {
LOG.error("create connection error", e);
setFailContinuous(true);
if (breakAfterAcquireFailure) {
break;
}
continue;
} catch (Error e) {
LOG.error("create connection error", e);
@ -2159,9 +2178,18 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
errorCount = 0; // reset errorCount
}
}
public boolean isStarted() {
return started;
}
public void setStarted(boolean started) {
this.started = started;
}
}
public class DestroyConnectionThread extends Thread {
private volatile boolean started = true;
public DestroyConnectionThread(String name) {
super(name);
@ -2195,6 +2223,13 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
}
public boolean isStarted() {
return started;
}
public void setStarted(boolean started) {
this.started = started;
}
}
public class DestroyTask implements Runnable {
@ -2313,7 +2348,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
return removeCount;
}
/** Instance key */
/**
* Instance key
*/
protected String instanceKey = null;
public Reference getReference() throws NamingException {
@ -3129,10 +3166,4 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
public boolean isClosed() {
return this.closed;
}
private void logActiveCount(boolean isIncrease){
if (logActiveCount){
LOG.debug("activeCount " + (isIncrease ? "increase" : "decrease") + "current count is : " + activeCount, new Throwable());
}
}
}

Loading…
Cancel
Save