mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -18,10 +18,12 @@ package com.alibaba.druid.pool;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.util.Utils;
|
||||
|
||||
import javax.sql.ConnectionEventListener;
|
||||
import javax.sql.StatementEventListener;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
@@ -232,7 +234,7 @@ public final class DruidConnectionHolder {
|
||||
|
||||
for (Object item : statementTrace.toArray()) {
|
||||
Statement stmt = (Statement) item;
|
||||
stmt.close();
|
||||
JdbcUtils.close(stmt);
|
||||
}
|
||||
statementTrace.clear();
|
||||
|
||||
|
||||
@@ -128,7 +128,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
// threads
|
||||
private ScheduledFuture<?> destroySchedulerFuture;
|
||||
private DestroyTask destoryTask;
|
||||
|
||||
|
||||
private CreateConnectionThread createConnectionThread;
|
||||
private DestroyConnectionThread destroyConnectionThread;
|
||||
private LogStatsThread logStatsThread;
|
||||
@@ -155,7 +155,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
public static ThreadLocal<Long> waitNanosLocal = new ThreadLocal<Long>();
|
||||
|
||||
private boolean logDifferentThread = true;
|
||||
|
||||
|
||||
public DruidDataSource(){
|
||||
this(false);
|
||||
}
|
||||
@@ -527,7 +527,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
this.jdbcUrl = this.jdbcUrl.trim();
|
||||
initFromWrapDriverUrl();
|
||||
}
|
||||
|
||||
|
||||
if (this.dbType == null || this.dbType.length() == 0) {
|
||||
this.dbType = JdbcUtils.getDbType(jdbcUrl, null);
|
||||
}
|
||||
@@ -671,17 +671,18 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
protected void createAndStartDestroyThread() {
|
||||
destoryTask = new DestroyTask();
|
||||
|
||||
|
||||
if (destroyScheduler != null) {
|
||||
long period = timeBetweenEvictionRunsMillis;
|
||||
if (period <= 0) {
|
||||
period = 1000;
|
||||
}
|
||||
destroySchedulerFuture = destroyScheduler.scheduleAtFixedRate(destoryTask, period, period, TimeUnit.MILLISECONDS);
|
||||
destroySchedulerFuture = destroyScheduler.scheduleAtFixedRate(destoryTask, period, period,
|
||||
TimeUnit.MILLISECONDS);
|
||||
initedLatch.countDown();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
String threadName = "Druid-ConnectionPool-Destroy-" + System.identityHashCode(this);
|
||||
destroyConnectionThread = new DestroyConnectionThread(threadName);
|
||||
destroyConnectionThread.start();
|
||||
@@ -694,7 +695,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
createConnectionThread.start();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
initedLatch.countDown();
|
||||
}
|
||||
|
||||
@@ -883,6 +884,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
private void initExceptionSorter() {
|
||||
if (this.exceptionSorter != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String realDriverClassName = driver.getClass().getName();
|
||||
if (realDriverClassName.equals(JdbcConstants.MYSQL_DRIVER)) {
|
||||
this.exceptionSorter = new MySqlExceptionSorter();
|
||||
@@ -1152,10 +1157,21 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
}
|
||||
this.discardConnection(holder.getConnection());
|
||||
holder.setDiscard(true);
|
||||
pooledConnection.disable(t);
|
||||
|
||||
boolean requireDiscard = false;
|
||||
synchronized (pooledConnection) {
|
||||
if ((!pooledConnection.isClosed()) || !pooledConnection.isDisable()) {
|
||||
holder.setDiscard(true);
|
||||
pooledConnection.disable(t);
|
||||
requireDiscard = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (requireDiscard) {
|
||||
this.discardConnection(holder.getConnection());
|
||||
holder.setDiscard(true);
|
||||
}
|
||||
|
||||
LOG.error("discard connection", sqlEx);
|
||||
}
|
||||
|
||||
@@ -1177,9 +1193,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
if (logDifferentThread //
|
||||
&& (!isAsyncCloseConnectionEnable()) //
|
||||
&& pooledConnection.getOwnerThread() != Thread.currentThread()//
|
||||
) {
|
||||
&& (!isAsyncCloseConnectionEnable()) //
|
||||
&& pooledConnection.getOwnerThread() != Thread.currentThread()//
|
||||
) {
|
||||
LOG.warn("get/close not same thread");
|
||||
}
|
||||
|
||||
@@ -1210,7 +1226,18 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
// reset holder, restore default settings, clear warnings
|
||||
holder.reset();
|
||||
boolean isSameThread = pooledConnection.getOwnerThread() == Thread.currentThread();
|
||||
if (!isSameThread) {
|
||||
synchronized (pooledConnection) {
|
||||
holder.reset();
|
||||
}
|
||||
} else {
|
||||
holder.reset();
|
||||
}
|
||||
|
||||
if (holder.isDiscard()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (testOnReturn) {
|
||||
boolean validate = testConnectionInternal(physicalConnection);
|
||||
@@ -1300,7 +1327,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
if (destroyConnectionThread != null) {
|
||||
destroyConnectionThread.interrupt();
|
||||
}
|
||||
|
||||
|
||||
if (destroySchedulerFuture != null) {
|
||||
destroySchedulerFuture.cancel(true);
|
||||
}
|
||||
@@ -1479,11 +1506,11 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
return last;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private final void decrementPoolingCount() {
|
||||
poolingCount--;
|
||||
}
|
||||
|
||||
|
||||
private final void incrementPoolingCount() {
|
||||
poolingCount++;
|
||||
}
|
||||
@@ -1670,7 +1697,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
public long getRemoveAbandonedCount() {
|
||||
return removeAbandonedCount;
|
||||
}
|
||||
|
||||
|
||||
protected void put(Connection connection) {
|
||||
DruidConnectionHolder holder = null;
|
||||
try {
|
||||
@@ -1692,10 +1719,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
notEmpty.signal();
|
||||
notEmptySignalCount++;
|
||||
|
||||
|
||||
if (createScheduler != null) {
|
||||
createTaskCount--;
|
||||
|
||||
|
||||
if (poolingCount + createTaskCount < notEmptyWaitThreadCount //
|
||||
&& activeCount + poolingCount + createTaskCount < maxActive) {
|
||||
emptySignal();
|
||||
@@ -1705,10 +1732,11 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class CreateConnectionTask implements Runnable {
|
||||
|
||||
private int errorCount = 0;
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (;;) {
|
||||
@@ -1843,7 +1871,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
put(connection);
|
||||
|
||||
|
||||
errorCount = 0; // reset errorCount
|
||||
}
|
||||
}
|
||||
@@ -1884,7 +1912,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public class DestroyTask implements Runnable {
|
||||
|
||||
@Override
|
||||
@@ -1895,7 +1923,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
removeAbandoned();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class LogStatsThread extends Thread {
|
||||
@@ -2556,7 +2584,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
public void setLogDifferentThread(boolean logDifferentThread) {
|
||||
this.logDifferentThread = logDifferentThread;
|
||||
}
|
||||
|
||||
|
||||
public DruidPooledConnection tryGetConnection() throws SQLException {
|
||||
if (poolingCount == 0) {
|
||||
return null;
|
||||
@@ -2646,7 +2674,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean isFull() {
|
||||
lock.lock();
|
||||
try {
|
||||
@@ -2655,21 +2683,21 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void emptySignal() {
|
||||
if (createScheduler == null) {
|
||||
empty.signal();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (createTaskCount >= maxCreateTaskCount) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (activeCount + poolingCount + createTaskCount >= maxActive) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
createTaskCount++;
|
||||
CreateConnectionTask task = new CreateConnectionTask();
|
||||
createScheduler.submit(task);
|
||||
|
||||
@@ -217,7 +217,14 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql.
|
||||
return;
|
||||
}
|
||||
|
||||
if (holder.getDataSource().isAsyncCloseConnectionEnable()) {
|
||||
DruidAbstractDataSource dataSource = holder.getDataSource();
|
||||
boolean isSameThread = this.getOwnerThread() == Thread.currentThread();
|
||||
|
||||
if (!isSameThread) {
|
||||
dataSource.setAsyncCloseConnectionEnable(true);
|
||||
}
|
||||
|
||||
if (dataSource.isAsyncCloseConnectionEnable()) {
|
||||
syncClose();
|
||||
return;
|
||||
}
|
||||
@@ -226,7 +233,7 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql.
|
||||
listener.connectionClosed(new ConnectionEvent(this));
|
||||
}
|
||||
|
||||
DruidAbstractDataSource dataSource = holder.getDataSource();
|
||||
|
||||
List<Filter> filters = dataSource.getProxyFilters();
|
||||
if (filters.size() > 0) {
|
||||
FilterChainImpl filterChain = new FilterChainImpl(dataSource);
|
||||
|
||||
@@ -40,6 +40,10 @@ public class Log4jImpl implements Log {
|
||||
public Log4jImpl(String loggerName){
|
||||
log = Logger.getLogger(loggerName);
|
||||
}
|
||||
|
||||
public Logger getLog() {
|
||||
return log;
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
return log.isDebugEnabled();
|
||||
|
||||
Reference in New Issue
Block a user