druid1.2.8源码阅读:第九天
一、DestroyTask线程
第一步: 获取锁
try {
lock.lockInterruptibly();
} catch (InterruptedException e) {
return;
}
第二步:判断有没有初始化
if (!inited) {
return;
}
keepAliveCount连接:
if (keepAliveCount > 0) {
// keep order
for (int i = keepAliveCount - 1; i >= 0; --i) {
DruidConnectionHolder holer = keepAliveConnections[i];
Connection connection = holer.getConnection();
holer.incrementKeepAliveCheckCount();
boolean validate = false;
//校验连接是否还可用
try {
this.validateConnection(connection);
validate = true;
} catch (Throwable error) {
if (LOG.isDebugEnabled()) {
LOG.debug("keepAliveErr", error);
}
// skip
}
boolean discard = !validate;
//如果可用,则直接put到connections中,放置到尾部。
if (validate) {
holer.lastKeepTimeMillis = System.currentTimeMillis();
boolean putOk = put(holer, 0L, true);
if (!putOk) {
discard = true;
}
}
//如果不可用,则关闭连接
if (discard) {
try {
connection.close();
} catch (Exception e) {
// skip
}
lock.lock();
//加锁更新计数器
try {
discardCount++;
if (activeCount + poolingCount <= minIdle) {
emptySignal();
}
} finally {
lock.unlock();
}
}
}
this.getDataSourceStat().addKeepAliveCheckCount(keepAliveCount);
Arrays.fill(keepAliveConnections, null);
}
对于keepalive状态的连接,当连接状态可用的时候,放回连接池。
另一种情况:连接不够用,并且连接池不满:
//keepAlive状态,且连接池中的连接加上被使用的连接仍然小于minIdle
if (keepAlive && poolingCount + activeCount < minIdle) {
needFill = true;
}