add stat PreparedStatementCacheAccessCount

This commit is contained in:
wenshao
2011-12-29 11:19:41 +08:00
parent 6f74a0ea86
commit ddfbf2b846
6 changed files with 79 additions and 58 deletions
@@ -183,7 +183,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
protected final AtomicLong commitCount = new AtomicLong();
protected final AtomicLong startTransactionCount = new AtomicLong();
protected final AtomicLong rollbackCount = new AtomicLong();
protected final AtomicLong reusePreparedStatement = new AtomicLong();
protected final AtomicLong cachedPreparedStatementHitCount = new AtomicLong();
protected final AtomicLong preparedStatementCount = new AtomicLong();
protected final AtomicLong closedPreparedStatementCount = new AtomicLong();
protected final AtomicLong cachedPreparedStatementCount = new AtomicLong();
@@ -201,10 +201,46 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
private ObjectName objectName;
protected Exception createError = null;
private final AtomicLong executeCount = new AtomicLong();
protected Throwable createError;
protected Throwable lastError;
protected long lastErrorTimeMillis;
protected Throwable lastCreateError;
protected long lastCreateErrorTimeMillis;
public Throwable getLastCreateError() {
return lastCreateError;
}
public Throwable getLastError() {
return this.lastError;
}
public long getLastErrorTimeMillis() {
return lastErrorTimeMillis;
}
public Date getLastErrorTime() {
if (lastErrorTimeMillis <= 0) {
return null;
}
return new Date(lastErrorTimeMillis);
}
public long getLastCreateErrorTimeMillis() {
return lastCreateErrorTimeMillis;
}
public Date getLastCreateErrorTime() {
if (lastErrorTimeMillis <= 0) {
return null;
}
return new Date(lastCreateErrorTimeMillis);
}
public int getTransactionQueryTimeout() {
if (transactionQueryTimeout <= 0) {
return queryTimeout;
@@ -225,10 +261,6 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
this.executeCount.incrementAndGet();
}
public Exception getCreateError() {
return createError;
}
public boolean isDupCloseLogEnable() {
return dupCloseLogEnable;
}
@@ -260,14 +292,18 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
public void incrementCachedPreparedStatementDeleteCount() {
cachedPreparedStatementDeleteCount.incrementAndGet();
}
public void incrementCachedPreparedStatementMissCount() {
cachedPreparedStatementMissCount.incrementAndGet();
}
public long getCachedPreparedStatementMissCount() {
return cachedPreparedStatementMissCount.get();
}
public long getCachedPreparedStatementAccessCount() {
return cachedPreparedStatementMissCount.get() + cachedPreparedStatementHitCount.get();
}
public long getCachedPreparedStatementDeleteCount() {
return cachedPreparedStatementDeleteCount.get();
@@ -293,12 +329,12 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
return preparedStatementCount.get();
}
public void incrementReusePreparedStatementCount() {
reusePreparedStatement.incrementAndGet();
public void incrementCachedPreparedStatementHitCount() {
cachedPreparedStatementHitCount.incrementAndGet();
}
public long getReusePreparedStatementCount() {
return reusePreparedStatement.get();
public long getCachedPreparedStatementHitCount() {
return cachedPreparedStatementHitCount.get();
}
public long getTransactionThresholdMillis() {
@@ -1205,10 +1241,14 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
} catch (SQLException ex) {
dataSource.createErrorCount++;
dataSource.createError = ex;
dataSource.lastCreateError = ex;
dataSource.lastCreateErrorTimeMillis = System.currentTimeMillis();
throw ex;
} catch (RuntimeException ex) {
dataSource.createErrorCount++;
dataSource.createError = ex;
dataSource.lastCreateError = ex;
dataSource.lastCreateErrorTimeMillis = System.currentTimeMillis();
throw ex;
} catch (Error ex) {
dataSource.createErrorCount++;
@@ -133,13 +133,17 @@ public interface DruidAbstractDataSourceMBean {
long getTransactionThresholdMillis();
long getReusePreparedStatementCount();
long getPreparedStatementCount();
long getClosedPreparedStatementCount();
long getCachedPreparedStatementCount();
long getCachedPreparedStatementDeleteCount();
long getCachedPreparedStatementAccessCount();
long getCachedPreparedStatementMissCount();
long getCachedPreparedStatementHitCount();
}
@@ -108,9 +108,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
private String initStackTrace;
private Throwable lastError;
private Throwable lastCreateError;
public DruidDataSource(){
}
@@ -126,14 +123,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
this.resetStatEnable = resetStatEnable;
}
public Throwable getLastCreateError() {
return lastCreateError;
}
public Throwable getLastError() {
return this.lastError;
}
public void restart() {
lock.lock();
try {
@@ -168,6 +157,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
activePeakTime = 0;
poolingPeak = 0;
createTimespan = 0;
lastError = null;
lastErrorTimeMillis = 0;
lastCreateError = null;
lastCreateErrorTimeMillis = 0;
} finally {
lock.unlock();
}
@@ -176,7 +169,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
commitCount.set(0);
rollbackCount.set(0);
startTransactionCount.set(0);
reusePreparedStatement.set(0);
cachedPreparedStatementHitCount.set(0);
closedPreparedStatementCount.set(0);
preparedStatementCount.set(0);
transactionHistogram.reset();
@@ -514,6 +507,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
errorCount.incrementAndGet();
lastError = t;
lastErrorTimeMillis = System.currentTimeMillis();
if (t instanceof SQLException) {
SQLException sqlEx = (SQLException) t;
@@ -878,13 +872,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
} catch (InterruptedException e) {
lastCreateError = e;
break;
} catch (RuntimeException e) {
lastCreateError = e;
LOG.error("create connection error", e);
} catch (Error e) {
lastCreateError = e;
LOG.error("create connection error", e);
lastErrorTimeMillis = System.currentTimeMillis();
break;
} finally {
lock.unlock();
@@ -186,8 +186,6 @@ public class PoolableConnection implements PooledConnection, Connection {
} catch (SQLException ex) {
handleException(ex);
}
} else {
holder.getDataSource().incrementReusePreparedStatementCount();
}
holder.getDataSource().initStatement(this, stmtHolder.getStatement());
@@ -225,8 +223,6 @@ public class PoolableConnection implements PooledConnection, Connection {
} catch (SQLException ex) {
handleException(ex);
}
} else {
holder.getDataSource().incrementReusePreparedStatementCount();
}
holder.getDataSource().initStatement(this, stmtHolder.getStatement());
@@ -265,8 +261,6 @@ public class PoolableConnection implements PooledConnection, Connection {
} catch (SQLException ex) {
handleException(ex);
}
} else {
holder.getDataSource().incrementReusePreparedStatementCount();
}
holder.getDataSource().initStatement(this, stmtHolder.getStatement());
@@ -301,8 +295,6 @@ public class PoolableConnection implements PooledConnection, Connection {
} catch (SQLException ex) {
handleException(ex);
}
} else {
holder.getDataSource().incrementReusePreparedStatementCount();
}
holder.getDataSource().initStatement(this, stmtHolder.getStatement());
@@ -337,8 +329,6 @@ public class PoolableConnection implements PooledConnection, Connection {
} catch (SQLException ex) {
handleException(ex);
}
} else {
holder.getDataSource().incrementReusePreparedStatementCount();
}
holder.getDataSource().initStatement(this, stmtHolder.getStatement());
@@ -373,8 +363,6 @@ public class PoolableConnection implements PooledConnection, Connection {
} catch (SQLException ex) {
handleException(ex);
}
} else {
holder.getDataSource().incrementReusePreparedStatementCount();
}
holder.getDataSource().initStatement(this, stmtHolder.getStatement());
@@ -411,8 +399,6 @@ public class PoolableConnection implements PooledConnection, Connection {
} catch (SQLException ex) {
handleException(ex);
}
} else {
holder.getDataSource().incrementReusePreparedStatementCount();
}
holder.getDataSource().initStatement(this, stmtHolder.getStatement());
@@ -451,8 +437,6 @@ public class PoolableConnection implements PooledConnection, Connection {
} catch (SQLException ex) {
handleException(ex);
}
} else {
holder.getDataSource().incrementReusePreparedStatementCount();
}
holder.getDataSource().initStatement(this, stmtHolder.getStatement());
@@ -489,8 +473,6 @@ public class PoolableConnection implements PooledConnection, Connection {
} catch (SQLException ex) {
handleException(ex);
}
} else {
holder.getDataSource().incrementReusePreparedStatementCount();
}
holder.getDataSource().initStatement(this, stmtHolder.getStatement());
@@ -49,6 +49,7 @@ public class PreparedStatementPool {
if (holder != null) {
holder.incrementReusedCount();
dataSource.incrementCachedPreparedStatementHitCount();
} else {
dataSource.incrementCachedPreparedStatementMissCount();
}
@@ -193,20 +193,24 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
map.put("NotEmptyWaitCount", dataSource.getNotEmptyWaitCount());
map.put("NotEmptyWaitNanos", dataSource.getNotEmptyWaitNanos());
map.put("ErrorCount", dataSource.getErrorCount());
map.put("ReusePreparedStatementCount", dataSource.getReusePreparedStatementCount());
map.put("ReusePreparedStatementCount", dataSource.getCachedPreparedStatementHitCount());
map.put("StartTransactionCount", dataSource.getStartTransactionCount());
map.put("CommitCount", dataSource.getCommitCount());
map.put("RollbackCount", dataSource.getRollbackCount());
map.put("LastError", JMXUtils.getErrorCompositeData(dataSource.getLastError()));
map.put("LastCreateError", JMXUtils.getErrorCompositeData(dataSource.getCreateError()));
map.put("LastCreateError", JMXUtils.getErrorCompositeData(dataSource.getLastCreateError()));
map.put("PreparedStatementCacheDeleteCount", dataSource.getCachedPreparedStatementDeleteCount());
map.put("PreparedStatementCacheAccessCount", dataSource.getCachedPreparedStatementAccessCount());
map.put("PreparedStatementCacheMissCount", dataSource.getCachedPreparedStatementMissCount());
map.put("PreparedStatementCacheHitCount", dataSource.getReusePreparedStatementCount());
map.put("PreparedStatementCacheHitCount", dataSource.getCachedPreparedStatementHitCount());
map.put("PreparedStatementCacheCurrentCount", dataSource.getCachedPreparedStatementCount());
map.put("Version", dataSource.getVersion());
map.put("LastErrorTime", dataSource.getLastErrorTime());
map.put("LastCreateErrorTime", dataSource.getLastCreateErrorTime());
return new CompositeDataSupport(rowType, map);
}
@@ -227,7 +231,8 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
SimpleType.STRING, SimpleType.INTEGER, SimpleType.STRING, SimpleType.STRING, SimpleType.LONG, //
SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG//
, SimpleType.LONG, SimpleType.LONG, JMXUtils.getThrowableCompositeType(), JMXUtils.getThrowableCompositeType(), SimpleType.LONG //
, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.STRING
, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.STRING //
, SimpleType.DATE , SimpleType.DATE
//
};
@@ -246,7 +251,8 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
"NotEmptyWaitCount", "NotEmptyWaitNanos", "ErrorCount", "ReusePreparedStatementCount",
"StartTransactionCount", //
"CommitCount", "RollbackCount", "LastError", "LastCreateError", "PreparedStatementCacheDeleteCount" //
, "PreparedStatementCacheMissCount", "PreparedStatementCacheHitCount", "PreparedStatementCacheCurrentCount", "Version"
, "PreparedStatementCacheAccessCount", "PreparedStatementCacheMissCount", "PreparedStatementCacheHitCount", "PreparedStatementCacheCurrentCount", "Version" //
, "LastErrorTime", "LastCreateErrorTime"
//
};