mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add testcase & bug fixed
This commit is contained in:
@@ -218,8 +218,6 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
|
||||
protected final ReentrantLock lock = new ReentrantLock(true);
|
||||
|
||||
protected int modCount = 0;
|
||||
|
||||
protected AtomicLong createCount = new AtomicLong();
|
||||
protected AtomicLong destroyCount = new AtomicLong();
|
||||
|
||||
@@ -227,10 +225,6 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
return isOracle;
|
||||
}
|
||||
|
||||
protected int getModCount() {
|
||||
return modCount;
|
||||
}
|
||||
|
||||
public void setOracle(boolean isOracle) {
|
||||
if (inited) {
|
||||
throw new IllegalStateException();
|
||||
@@ -1298,9 +1292,9 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
|
||||
public void initPhysicalConnection(Connection conn) throws SQLException {
|
||||
if (conn.getAutoCommit() != defaultAutoCommit) {
|
||||
conn.setAutoCommit(defaultAutoCommit);
|
||||
conn.setAutoCommit(defaultAutoCommit);
|
||||
}
|
||||
|
||||
|
||||
if (getDefaultReadOnly() != null) {
|
||||
if (conn.isReadOnly() != getDefaultReadOnly()) {
|
||||
conn.setReadOnly(getDefaultReadOnly());
|
||||
|
||||
@@ -56,10 +56,8 @@ public final class DruidConnectionHolder {
|
||||
private int underlyingHoldability;
|
||||
private int underlyingTransactionIsolation;
|
||||
private boolean underlyingAutoCommit;
|
||||
private final int modCount;
|
||||
|
||||
public DruidConnectionHolder(DruidAbstractDataSource dataSource, Connection conn) throws SQLException{
|
||||
this.modCount = dataSource.getModCount();
|
||||
|
||||
this.dataSource = dataSource;
|
||||
this.conn = conn;
|
||||
@@ -82,10 +80,6 @@ public final class DruidConnectionHolder {
|
||||
this.defaultReadOnly = underlyingReadOnly;
|
||||
}
|
||||
|
||||
public int getModCount() {
|
||||
return modCount;
|
||||
}
|
||||
|
||||
public boolean isUnderlyingReadOnly() {
|
||||
return underlyingReadOnly;
|
||||
}
|
||||
|
||||
@@ -637,9 +637,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
if (isRemoveAbandoned()) {
|
||||
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
||||
activeConnections.put(poolalbeConnection,
|
||||
new ActiveConnectionTraceInfo(poolalbeConnection, System.currentTimeMillis(),
|
||||
stackTrace));
|
||||
activeConnections.put(poolalbeConnection, new ActiveConnectionTraceInfo(System.currentTimeMillis(),
|
||||
stackTrace));
|
||||
poolalbeConnection.setTraceEnable(true);
|
||||
}
|
||||
|
||||
@@ -768,7 +767,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
* 回收连接
|
||||
*/
|
||||
protected void recycle(DruidPooledConnection pooledConnection) throws SQLException {
|
||||
final Connection conn = pooledConnection.getConnection();
|
||||
final DruidConnectionHolder holder = pooledConnection.getConnectionHolder();
|
||||
|
||||
if (holder == null) {
|
||||
@@ -776,6 +774,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
return;
|
||||
}
|
||||
|
||||
final Connection physicalConnection = holder.getConnection();
|
||||
|
||||
if (pooledConnection.isTraceEnable()) {
|
||||
ActiveConnectionTraceInfo oldInfo = activeConnections.remove(pooledConnection);
|
||||
if (oldInfo == null) {
|
||||
@@ -786,18 +786,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
try {
|
||||
// 第一步,检查连接是否关闭
|
||||
if (conn == null) {
|
||||
lock.lockInterruptibly();
|
||||
try {
|
||||
activeCount--;
|
||||
closeCount++;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
final boolean isAutoCommit = holder.isUnderlyingAutoCommit();
|
||||
final boolean isReadOnly = holder.isUnderlyingReadOnly();
|
||||
|
||||
@@ -810,9 +798,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
holder.reset();
|
||||
|
||||
if (isTestOnReturn()) {
|
||||
boolean validate = testConnectionInternal(conn);
|
||||
boolean validate = testConnectionInternal(physicalConnection);
|
||||
if (!validate) {
|
||||
JdbcUtils.close(conn);
|
||||
JdbcUtils.close(physicalConnection);
|
||||
|
||||
destroyCount.incrementAndGet();
|
||||
|
||||
@@ -830,28 +818,21 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
boolean neadDestory = false;
|
||||
lock.lockInterruptibly();
|
||||
try {
|
||||
if (holder.getModCount() == this.modCount) {
|
||||
activeCount--;
|
||||
closeCount++;
|
||||
activeCount--;
|
||||
closeCount++;
|
||||
|
||||
// 第六步,加入队列中(putLast)
|
||||
putLast(holder);
|
||||
recycleCount++;
|
||||
} else {
|
||||
activeCount--;
|
||||
closeCount++;
|
||||
neadDestory = true;
|
||||
}
|
||||
putLast(holder);
|
||||
recycleCount++;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
if (neadDestory) {
|
||||
destroyCount.incrementAndGet();
|
||||
JdbcUtils.close(conn);
|
||||
JdbcUtils.close(physicalConnection);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
JdbcUtils.close(conn);
|
||||
JdbcUtils.close(physicalConnection);
|
||||
|
||||
try {
|
||||
lock.lockInterruptibly();
|
||||
@@ -974,11 +955,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
notEmptyWaitThreadCount--;
|
||||
}
|
||||
notEmptyWaitCount++;
|
||||
|
||||
if (!enable) {
|
||||
connectErrorCount.incrementAndGet();
|
||||
throw new DataSourceDisableException();
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException ie) {
|
||||
notEmpty.signal(); // propagate to non-interrupted thread
|
||||
@@ -1017,11 +993,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
// creator
|
||||
notEmptyWaitCount++;
|
||||
notEmptyWaitNanos += (startEstimate - estimate);
|
||||
|
||||
if (!enable) {
|
||||
connectErrorCount.incrementAndGet();
|
||||
throw new DataSourceDisableException();
|
||||
}
|
||||
} catch (InterruptedException ie) {
|
||||
notEmpty.signal(); // propagate to non-interrupted thread
|
||||
notEmptySignalCount++;
|
||||
@@ -1310,10 +1281,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
return removeCount;
|
||||
}
|
||||
|
||||
public DataSourceProxyConfig getConfig() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Instance key */
|
||||
protected String instanceKey = null;
|
||||
|
||||
@@ -1327,22 +1294,15 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
static class ActiveConnectionTraceInfo {
|
||||
|
||||
private final DruidPooledConnection connection;
|
||||
private final long connectTime;
|
||||
private final StackTraceElement[] stackTrace;
|
||||
private final long connectTime;
|
||||
private final StackTraceElement[] stackTrace;
|
||||
|
||||
public ActiveConnectionTraceInfo(DruidPooledConnection connection, long connectTime,
|
||||
StackTraceElement[] stackTrace){
|
||||
public ActiveConnectionTraceInfo(long connectTime, StackTraceElement[] stackTrace){
|
||||
super();
|
||||
this.connection = connection;
|
||||
this.connectTime = connectTime;
|
||||
this.stackTrace = stackTrace;
|
||||
}
|
||||
|
||||
public DruidPooledConnection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public long getConnectTime() {
|
||||
return connectTime;
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
protected TransactionInfo transactionInfo;
|
||||
private final boolean dupCloseLogEnable;
|
||||
private boolean traceEnable = false;
|
||||
private boolean diable = false;
|
||||
private boolean disable = false;
|
||||
private boolean closed = false;
|
||||
private final Thread ownerThread;
|
||||
|
||||
@@ -157,20 +157,20 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
return conn;
|
||||
}
|
||||
|
||||
void disable() {
|
||||
public void disable() {
|
||||
this.traceEnable = false;
|
||||
this.holder = null;
|
||||
this.transactionInfo = null;
|
||||
this.diable = true;
|
||||
this.disable = true;
|
||||
}
|
||||
|
||||
public boolean isDiable() {
|
||||
return diable;
|
||||
public boolean isDisable() {
|
||||
return disable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
if (this.diable) {
|
||||
if (this.disable) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -194,16 +194,20 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
} else {
|
||||
recycle();
|
||||
}
|
||||
|
||||
this.disable = true;
|
||||
}
|
||||
|
||||
public void recycle() throws SQLException {
|
||||
if (this.diable) {
|
||||
if (this.disable) {
|
||||
return;
|
||||
}
|
||||
|
||||
DruidConnectionHolder holder = this.holder;
|
||||
if (holder == null && dupCloseLogEnable) {
|
||||
LOG.error("dup close");
|
||||
if (holder == null) {
|
||||
if (dupCloseLogEnable) {
|
||||
LOG.error("dup close");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -972,7 +976,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
}
|
||||
|
||||
public void checkState() throws SQLException {
|
||||
if (holder == null || closed || diable) {
|
||||
if (holder == null || closed || disable) {
|
||||
throw new SQLException("connection is closed");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user