mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
支持testWhileIdle & idleCheck
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@389 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -143,6 +143,15 @@ public abstract class DruidAbstractDataSource implements DruidAbstractDataSource
|
||||
protected ValidConnectionChecker validConnectionChecker = null;
|
||||
|
||||
protected final AtomicLong errorCount = new AtomicLong();
|
||||
protected final AtomicLong dupCloseCount = new AtomicLong();
|
||||
|
||||
public long getDupCloseCount() {
|
||||
return dupCloseCount.get();
|
||||
}
|
||||
|
||||
public void incrementDupCloseCount() {
|
||||
dupCloseCount.incrementAndGet();
|
||||
}
|
||||
|
||||
public ValidConnectionChecker getValidConnectionChecker() {
|
||||
return validConnectionChecker;
|
||||
|
||||
@@ -251,6 +251,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
if (isTestOnBorrow()) {
|
||||
boolean validate = testConnection(poolalbeConnection.getRawConnection());
|
||||
if (!validate) {
|
||||
LOG.debug("skip not validate connection.");
|
||||
Connection realConnection = poolalbeConnection.getConnection();
|
||||
JdbcUtils.close(realConnection);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -355,7 +358,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
if (count >= maxIdle) {
|
||||
lock.lock();
|
||||
try {
|
||||
conn.close();
|
||||
JdbcUtils.close(conn);
|
||||
destroyCount++;
|
||||
closeCount++;
|
||||
decrementActiveCount();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
@@ -367,6 +372,15 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
if (isTestOnReturn()) {
|
||||
boolean validate = testConnection(conn);
|
||||
if (!validate) {
|
||||
lock.lock();
|
||||
try {
|
||||
JdbcUtils.close(conn);
|
||||
destroyCount++;
|
||||
closeCount++;
|
||||
decrementActiveCount();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -640,6 +654,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
try {
|
||||
if (timeBetweenEvictionRunsMillis > 0) {
|
||||
Thread.sleep(timeBetweenEvictionRunsMillis);
|
||||
} else {
|
||||
Thread.sleep(100); //
|
||||
}
|
||||
|
||||
if (Thread.interrupted()) {
|
||||
@@ -685,11 +701,12 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
if (evictCount > 0) {
|
||||
for (int i = 0; i < evictCount; ++i) {
|
||||
evictList.add(connections[i]);
|
||||
}
|
||||
System.arraycopy(connections, evictCount, connections, 0, count - evictCount);
|
||||
for (int i = 0; i < evictCount; ++i) {
|
||||
int index = --count;
|
||||
evictList.add(connections[index]);
|
||||
connections[index] = null;
|
||||
connections[--count] = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -697,7 +714,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
ConnectionHolder[] idleConnections = new ConnectionHolder[idleCount];
|
||||
System.arraycopy(connections, 0, idleConnections, 0, idleCount);
|
||||
System.arraycopy(connections, idleCount, connections, 0, count - idleCount);
|
||||
for (int i = 0; i < evictCount; ++i) {
|
||||
for (int i = 0; i < idleCount; ++i) {
|
||||
connections[--count] = null;
|
||||
}
|
||||
|
||||
@@ -705,10 +722,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
if (testConnection(idleConnection.getConnection())) {
|
||||
connections[count++] = idleConnection;
|
||||
idleConnection.setLastActiveMillis(System.currentTimeMillis());
|
||||
idleCheckCount++;
|
||||
} else {
|
||||
evictList.add(idleConnection);
|
||||
}
|
||||
idleCheckCount++;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
@@ -719,12 +736,12 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
Connection connection = item.getConnection();
|
||||
try {
|
||||
connection.close();
|
||||
destroyCount++;
|
||||
} catch (SQLException e) {
|
||||
LOG.error("create connection error", e);
|
||||
}
|
||||
}
|
||||
|
||||
destroyCount++;
|
||||
evictList.clear();
|
||||
} catch (InterruptedException e) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -38,6 +38,8 @@ import javax.sql.ConnectionEventListener;
|
||||
import javax.sql.PooledConnection;
|
||||
import javax.sql.StatementEventListener;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.pool.PoolablePreparedStatement.PreparedStatementKey;
|
||||
import com.alibaba.druid.pool.PreparedStatementPool.MethodType;
|
||||
|
||||
@@ -46,6 +48,8 @@ import com.alibaba.druid.pool.PreparedStatementPool.MethodType;
|
||||
*/
|
||||
public class PoolableConnection implements PooledConnection, Connection {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(PoolableConnection.class);
|
||||
|
||||
protected Connection conn;
|
||||
protected ConnectionHolder holder;
|
||||
|
||||
@@ -116,6 +120,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
if (holder == null) {
|
||||
LOG.warn("dup close");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -156,8 +161,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
|
||||
throws SQLException {
|
||||
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
|
||||
checkOpen();
|
||||
|
||||
if (holder.isPoolPreparedStatements()) {
|
||||
@@ -181,8 +185,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
|
||||
int resultSetHoldability) throws SQLException {
|
||||
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
|
||||
checkOpen();
|
||||
|
||||
if (holder.isPoolPreparedStatements()) {
|
||||
@@ -195,8 +198,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
return poolableStatement;
|
||||
}
|
||||
|
||||
PreparedStatement stmt = conn.prepareStatement(sql, resultSetType, resultSetConcurrency,
|
||||
resultSetHoldability);
|
||||
PreparedStatement stmt = conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
|
||||
|
||||
holder.getDataSource().initStatement(stmt);
|
||||
|
||||
@@ -305,8 +307,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
|
||||
int resultSetHoldability) throws SQLException {
|
||||
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
|
||||
checkOpen();
|
||||
|
||||
if (holder.isPoolPreparedStatements()) {
|
||||
@@ -370,8 +371,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
|
||||
throws SQLException {
|
||||
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
|
||||
checkOpen();
|
||||
|
||||
Statement stmt = conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
|
||||
|
||||
Reference in New Issue
Block a user