mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add method : DruidDataSource.discardConnection(ConnectionHolder)
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>1.1.20</version>
|
||||
<version>1.1.21_preview_01</version>
|
||||
|
||||
|
||||
<packaging>jar</packaging>
|
||||
@@ -21,7 +21,7 @@
|
||||
<javadoc.skip>true</javadoc.skip>
|
||||
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<jdk.version>1.6</jdk.version>
|
||||
<jdk.version>1.8</jdk.version>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
|
||||
@@ -19,7 +19,7 @@ public final class VERSION {
|
||||
|
||||
public final static int MajorVersion = 1;
|
||||
public final static int MinorVersion = 1;
|
||||
public final static int RevisionVersion = 20;
|
||||
public final static int RevisionVersion = 21;
|
||||
|
||||
public static String getVersionNumber() {
|
||||
return VERSION.MajorVersion + "." + VERSION.MinorVersion + "." + VERSION.RevisionVersion;
|
||||
|
||||
@@ -1473,7 +1473,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
long mysqlIdleMillis = currentTimeMillis - lastPacketReceivedTimeMs;
|
||||
if (lastPacketReceivedTimeMs > 0 //
|
||||
&& mysqlIdleMillis >= timeBetweenEvictionRunsMillis) {
|
||||
discardConnection(conn);
|
||||
discardConnection(holder);
|
||||
String errorMsg = "discard long time none received connection. "
|
||||
+ ", jdbcUrl : " + jdbcUrl
|
||||
+ ", jdbcUrl : " + jdbcUrl
|
||||
@@ -2064,7 +2064,10 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
}
|
||||
|
||||
public abstract void discardConnection(Connection realConnection);
|
||||
|
||||
|
||||
public void discardConnection(DruidConnectionHolder holder) {
|
||||
discardConnection(holder.getConnection());
|
||||
}
|
||||
|
||||
public boolean isAsyncCloseConnectionEnable() {
|
||||
if (isRemoveAbandoned()) {
|
||||
|
||||
@@ -68,7 +68,7 @@ public final class DruidConnectionHolder {
|
||||
protected int underlyingHoldability;
|
||||
protected int underlyingTransactionIsolation;
|
||||
protected boolean underlyingAutoCommit;
|
||||
protected boolean discard = false;
|
||||
protected volatile boolean discard = false;
|
||||
protected final Map<String, Object> variables;
|
||||
protected final Map<String, Object> globleVariables;
|
||||
final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
@@ -1418,14 +1418,12 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
LOG.debug("skip not validate connection.");
|
||||
}
|
||||
|
||||
Connection realConnection = poolableConnection.conn;
|
||||
discardConnection(realConnection);
|
||||
discardConnection(poolableConnection.holder);
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
Connection realConnection = poolableConnection.conn;
|
||||
if (poolableConnection.conn.isClosed()) {
|
||||
discardConnection(null); // 传入null,避免重复关闭
|
||||
discardConnection(poolableConnection.holder); // 传入null,避免重复关闭
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1462,7 +1460,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
LOG.debug("skip not validate connection.");
|
||||
}
|
||||
|
||||
discardConnection(realConnection);
|
||||
discardConnection(poolableConnection.holder);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -1512,6 +1510,35 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
|
||||
public void discardConnection(DruidConnectionHolder holder) {
|
||||
if (holder == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Connection conn = holder.getConnection();
|
||||
if (conn != null) {
|
||||
JdbcUtils.close(conn);
|
||||
}
|
||||
|
||||
lock.lock();
|
||||
try {
|
||||
if (holder.discard) {
|
||||
return;
|
||||
}
|
||||
|
||||
activeCount--;
|
||||
discardCount++;
|
||||
|
||||
holder.discard = true;
|
||||
|
||||
if (activeCount <= minIdle) {
|
||||
emptySignal();
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private DruidPooledConnection getConnectionInternal(long maxWait) throws SQLException {
|
||||
if (closed) {
|
||||
connectErrorCountUpdater.incrementAndGet(this);
|
||||
@@ -1761,7 +1788,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
lock.lock();
|
||||
try {
|
||||
if ((!conn.isClosed()) || !conn.isDisable()) {
|
||||
holder.setDiscard(true);
|
||||
conn.disable(error);
|
||||
requireDiscard = true;
|
||||
}
|
||||
@@ -1800,8 +1826,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
|
||||
this.discardConnection(holder.getConnection());
|
||||
holder.setDiscard(true);
|
||||
this.discardConnection(holder);
|
||||
}
|
||||
|
||||
LOG.error("discard connection", error);
|
||||
@@ -1874,7 +1899,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
if (phyMaxUseCount > 0 && holder.useCount >= phyMaxUseCount) {
|
||||
discardConnection(holder.conn);
|
||||
discardConnection(holder);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1908,7 +1933,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
if (!enable) {
|
||||
discardConnection(holder.conn);
|
||||
discardConnection(holder);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1918,7 +1943,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
if (phyTimeoutMillis > 0) {
|
||||
long phyConnectTimeMillis = currentTimeMillis - holder.connectTimeMillis;
|
||||
if (phyConnectTimeMillis > phyTimeoutMillis) {
|
||||
discardConnection(holder.conn);
|
||||
discardConnection(holder);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -1942,7 +1967,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
holder.clearStatementCache();
|
||||
|
||||
if (!holder.discard) {
|
||||
this.discardConnection(physicalConnection);
|
||||
discardConnection(holder);
|
||||
holder.discard = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -85,7 +85,12 @@ public final class JdbcUtils implements JdbcConstants {
|
||||
if (x == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (x.isClosed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
x.close();
|
||||
} catch (Exception e) {
|
||||
LOG.debug("close connection error", e);
|
||||
|
||||
Reference in New Issue
Block a user