mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
DruidDataSource add keepAlive support.
This commit is contained in:
@@ -119,8 +119,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
private long notEmptyWaitNanos = 0L;
|
||||
|
||||
private int keepAliveCheckCount = 0;
|
||||
private final AtomicInteger keepAliveCheckOkCount = new AtomicInteger();
|
||||
private final AtomicInteger keepAliveCheckErrCount = new AtomicInteger();
|
||||
|
||||
private int activePeak = 0;
|
||||
private long activePeakTime = 0;
|
||||
@@ -1747,6 +1745,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
value.setWaitThreadCount(lock.getWaitQueueLength(notEmpty));
|
||||
value.setNotEmptyWaitCount(this.notEmptyWaitCount);
|
||||
value.setNotEmptyWaitNanos(this.notEmptyWaitNanos);
|
||||
value.setKeepAliveCheckCount(this.keepAliveCheckCount);
|
||||
|
||||
// reset
|
||||
this.poolingPeak = 0;
|
||||
@@ -1755,6 +1754,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
this.activePeakTime = 0;
|
||||
this.connectCount = 0;
|
||||
this.closeCount = 0;
|
||||
this.keepAliveCheckCount = 0;
|
||||
|
||||
this.notEmptyWaitCount = 0;
|
||||
this.notEmptyWaitNanos = 0;
|
||||
@@ -2365,6 +2365,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
if (keepAliveCount > 0) {
|
||||
this.getDataSourceStat().addKeepAliveCheckCount(keepAliveCount);
|
||||
for (int i = 0; i < keepAliveCount; ++i) {
|
||||
DruidConnectionHolder holer = keepAliveConnections[i];
|
||||
Connection connection = holer.getConnection();
|
||||
@@ -2383,9 +2384,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
if (validate) {
|
||||
holer.setLastActiveTimeMillis(System.currentTimeMillis());
|
||||
put(holer);
|
||||
keepAliveCheckOkCount.incrementAndGet();
|
||||
} else {
|
||||
keepAliveCheckErrCount.incrementAndGet();
|
||||
JdbcUtils.close(connection);
|
||||
}
|
||||
}
|
||||
@@ -2699,6 +2698,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
map.put("CreateErrorCount", this.getCreateErrorCount());
|
||||
map.put("DiscardCount", this.getDiscardCount());
|
||||
|
||||
|
||||
return map;
|
||||
} catch (JMException ex) {
|
||||
throw new IllegalStateException("getStatData error", ex);
|
||||
@@ -2797,6 +2797,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
dataMap.put("RemoveAbandoned", this.isRemoveAbandoned());
|
||||
dataMap.put("ClobOpenCount", this.getDataSourceStat().getClobOpenCount());
|
||||
dataMap.put("BlobOpenCount", this.getDataSourceStat().getBlobOpenCount());
|
||||
dataMap.put("KeepAliveCheckCount", this.getDataSourceStat().getKeepAliveCheckCount());
|
||||
|
||||
return dataMap;
|
||||
}
|
||||
|
||||
@@ -234,6 +234,10 @@ public class DruidDataSourceStatLoggerImpl extends DruidDataSourceStatLoggerAdap
|
||||
map.put("sqlList", sqlList);
|
||||
}
|
||||
|
||||
if (statValue.getKeepAliveCheckCount() > 0) {
|
||||
map.put("keepAliveCheckCount", statValue.getKeepAliveCheckCount());
|
||||
}
|
||||
|
||||
String text = JSONUtils.toJSONString(map);
|
||||
|
||||
log(text);
|
||||
|
||||
@@ -117,6 +117,9 @@ public class DruidDataSourceStatValue {
|
||||
@MField(aggregate = AggregateType.Sum)
|
||||
protected long startTransactionCount;
|
||||
|
||||
@MField(aggregate = AggregateType.Sum)
|
||||
protected long keepAliveCheckCount;
|
||||
|
||||
// protected long[] transactionHistogram;
|
||||
protected long[] connectionHoldTimeHistogram;
|
||||
|
||||
@@ -563,4 +566,11 @@ public class DruidDataSourceStatValue {
|
||||
this.poolingPeakTime = poolingPeakTime;
|
||||
}
|
||||
|
||||
public long getKeepAliveCheckCount() {
|
||||
return keepAliveCheckCount;
|
||||
}
|
||||
|
||||
public void setKeepAliveCheckCount(long keepAliveCheckCount) {
|
||||
this.keepAliveCheckCount = keepAliveCheckCount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,6 +73,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
private final AtomicLong clobOpenCount = new AtomicLong();
|
||||
|
||||
private final AtomicLong blobOpenCount = new AtomicLong();
|
||||
private final AtomicLong keepAliveCheckCount = new AtomicLong();
|
||||
|
||||
private boolean resetStatEnable = true;
|
||||
|
||||
@@ -497,4 +498,15 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
blobOpenCount.incrementAndGet();
|
||||
}
|
||||
|
||||
public long getKeepAliveCheckCount() {
|
||||
return keepAliveCheckCount.get();
|
||||
}
|
||||
|
||||
public long getKeepAliveCheckCountAndReset() {
|
||||
return keepAliveCheckCount.getAndSet(0);
|
||||
}
|
||||
|
||||
public void addKeepAliveCheckCount(long delta) {
|
||||
keepAliveCheckCount.addAndGet(delta);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,13 +268,18 @@
|
||||
<td>{{=datasourceNow.BlobOpenCount}}</td>
|
||||
<td class="lang" langKey="BlobOpenCountDesc"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td valign="top" class="td_lable" > <span class="lang" langKey="KeepAliveCheckCount">KeepAliveCheckCount</span></td>
|
||||
<td>{{=datasourceNow.KeepAliveCheckCount}}</td>
|
||||
<td class="lang" langKey="KeepAliveCheckCount"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td valign="top" class="td_lable"><span class="lang" langKey="ActiveConnectionStackTrace">ActiveConnection StackTrace</span></td>
|
||||
<td>
|
||||
<a href="activeConnectionStackTrace.html?datasourceId={{=datasourceNow.Identity}}">View</a>
|
||||
</td>
|
||||
<td>
|
||||
StackTrace for active Connection.
|
||||
StackTrace for active Connection.
|
||||
<a href="activeConnectionStackTrace-{{=datasourceNow.Identity}}.json" target="_blank">[View JSON API]</a>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -73,6 +73,7 @@ druid.lang = function() {
|
||||
'ConnectionHoldTimeHistogram' : ['ConnectionHoldTimeHistogram', '连接持有时间分布'],
|
||||
'ClobOpenCount' : ['ClobOpenCount', 'Clob打开次数'],
|
||||
'BlobOpenCount' : ['BlobOpenCount', 'Blob打开次数'],
|
||||
'KeepAliveCheckCount' : ['KeepAliveCheckCount', 'KeepAlive检测次数'],
|
||||
'ActiveConnectionStackTrace' : ['ActiveConnection StackTrace', '活跃连接堆栈查看'],
|
||||
'PollingConnectionInfo' : ['PollingConnection Info', '连接池中连接信息'],
|
||||
'SQLList' : ['SQL List', 'sql列表'],
|
||||
|
||||
@@ -107,7 +107,7 @@ public class TestIdle extends TestCase {
|
||||
|
||||
Thread.sleep(dataSource.getMinEvictableIdleTimeMillis() * 2);
|
||||
Assert.assertEquals(0, driver.getConnections().size());
|
||||
Assert.assertEquals(0, dataSource.getPoolingCount());
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
Assert.assertEquals(1, dataSource.getActiveCount());
|
||||
|
||||
Reference in New Issue
Block a user