optimized DruidDataSource.getStatData lock

This commit is contained in:
wenshao
2012-10-01 16:58:07 +08:00
parent 69a751e85c
commit 7a7af668f3
2 changed files with 53 additions and 26 deletions
@@ -138,7 +138,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
protected volatile int queryTimeout;
protected volatile int transactionQueryTimeout;
protected long createErrorCount;
protected AtomicLong createErrorCount = new AtomicLong();
protected long createTimespan;
@@ -205,11 +205,11 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
private final AtomicLong executeCount = new AtomicLong();
protected Throwable createError;
protected Throwable lastError;
protected long lastErrorTimeMillis;
protected Throwable lastCreateError;
protected long lastCreateErrorTimeMillis;
protected volatile Throwable createError;
protected volatile Throwable lastError;
protected volatile long lastErrorTimeMillis;
protected volatile Throwable lastCreateError;
protected volatile long lastCreateErrorTimeMillis;
protected boolean isOracle = false;
@@ -839,7 +839,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
public long getCreateErrorCount() {
return createErrorCount;
return createErrorCount.get();
}
public int getMaxActive() {
@@ -1269,19 +1269,19 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
validateConnection(conn);
createError = null;
} catch (SQLException ex) {
createErrorCount++;
createErrorCount.incrementAndGet();
createError = ex;
lastCreateError = ex;
lastCreateErrorTimeMillis = System.currentTimeMillis();
throw ex;
} catch (RuntimeException ex) {
createErrorCount++;
createErrorCount.incrementAndGet();
createError = ex;
lastCreateError = ex;
lastCreateErrorTimeMillis = System.currentTimeMillis();
throw ex;
} catch (Error ex) {
createErrorCount++;
createErrorCount.incrementAndGet();
throw ex;
} finally {
long nano = System.nanoTime() - startNano;
@@ -127,9 +127,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
public DruidDataSource(){
this(true);
}
public DruidDataSource(boolean fairLock){
super (fairLock);
super(fairLock);
}
public String getInitStackTrace() {
@@ -663,7 +663,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
* @param realConnection
* @throws SQLException
*/
private void discardConnection(Connection realConnection) throws SQLException {
private void discardConnection(Connection realConnection) {
JdbcUtils.close(realConnection);
lock.lock();
@@ -789,10 +789,11 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
}
try {
final boolean isAutoCommit = holder.isUnderlyingAutoCommit();
final boolean isReadOnly = holder.isUnderlyingReadOnly();
final boolean isAutoCommit = holder.isUnderlyingAutoCommit();
final boolean isReadOnly = holder.isUnderlyingReadOnly();
final boolean testOnReturn = this.isTestOnReturn();
try {
// check need to rollback?
if ((!isAutoCommit) && (!isReadOnly)) {
pooledConnection.rollback();
@@ -801,7 +802,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
// reset holder, restore default settings, clear warnings
holder.reset();
if (isTestOnReturn()) {
if (testOnReturn) {
boolean validate = testConnectionInternal(physicalConnection);
if (!validate) {
JdbcUtils.close(physicalConnection);
@@ -1188,7 +1189,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
LOG.error("create connection holder error", ex);
break;
}
lock.lock();
try {
connections[poolingCount++] = holder;
@@ -1725,6 +1726,32 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
public Map<String, Object> getStatData() {
final int activeCount;
final int activePeak;
final Date activePeakTime;
final int poolingCount;
final int poolingPeak;
final Date poolingPeakTime;
final long connectCount;
final long closeCount;
lock.lock();
try {
poolingCount = this.poolingCount;
poolingPeak = this.poolingPeak;
poolingPeakTime = this.getPoolingPeakTime();
activeCount = this.activeCount;
activePeak = this.activePeak;
activePeakTime = this.getActivePeakTime();
connectCount = this.connectCount;
closeCount = this.closeCount;
} finally {
lock.unlock();
}
Map<String, Object> dataMap = new LinkedHashMap<String, Object>();
dataMap.put("Identity", System.identityHashCode(this));
@@ -1740,13 +1767,13 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
dataMap.put("NotEmptyWaitCount", this.getNotEmptyWaitCount());
dataMap.put("NotEmptyWaitMillis", this.getNotEmptyWaitMillis());
dataMap.put("PoolingCount", this.getPoolingCount());
dataMap.put("PoolingPeak", this.getPoolingPeak());
dataMap.put("PoolingPeakTime", this.getPoolingPeakTime() == null ? null : this.getPoolingPeakTime().toString());
dataMap.put("PoolingCount", poolingCount);
dataMap.put("PoolingPeak", poolingPeak);
dataMap.put("PoolingPeakTime", poolingPeakTime);
dataMap.put("ActiveCount", this.getActiveCount());
dataMap.put("ActivePeak", this.getActivePeak());
dataMap.put("ActivePeakTime", this.getActivePeakTime() == null ? null : this.getActivePeakTime().toString());
dataMap.put("ActiveCount", activeCount);
dataMap.put("ActivePeak", activePeak);
dataMap.put("ActivePeakTime", activePeakTime);
dataMap.put("InitialSize", this.getInitialSize());
dataMap.put("MinIdle", this.getMinIdle());
@@ -1766,8 +1793,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
dataMap.put("DefaultReadOnly", this.isDefaultAutoCommit());
dataMap.put("DefaultTransactionIsolation", this.getDefaultTransactionIsolation());
dataMap.put("LogicConnectCount", this.getConnectCount());
dataMap.put("LogicCloseCount", this.getCloseCount());
dataMap.put("LogicConnectCount", connectCount);
dataMap.put("LogicCloseCount", closeCount);
dataMap.put("LogicConnectErrorCount", this.getConnectErrorCount());
dataMap.put("PhysicalConnectCount", this.getCreateCount());