写点什么

Druid 连接池源码阅读 05

作者:石小天
  • 2022 年 5 月 15 日
  • 本文字数:1240 字

    阅读完需:约 4 分钟

如果连接池内没有连接了,则调用 empty.signal(),通知 CreateThread 创建连接,并且等待指定的时间,被唤醒之后再去查看是否有可用连接。

private DruidConnectionHolder pollLast(long nanos) throws InterruptedException, SQLException {        long estimate = nanos;
for (;;) { if (poolingCount == 0) { emptySignal(); // send signal to CreateThread create connection
if (failFast && isFailContinuous()) { throw new DataSourceNotAvailableException(createError); }
if (estimate <= 0) { waitNanosLocal.set(nanos - estimate); return null; }
notEmptyWaitThreadCount++; if (notEmptyWaitThreadCount > notEmptyWaitThreadPeak) { notEmptyWaitThreadPeak = notEmptyWaitThreadCount; }
try { long startEstimate = estimate; estimate = notEmpty.awaitNanos(estimate); // signal by // recycle or // creator notEmptyWaitCount++; notEmptyWaitNanos += (startEstimate - estimate);
if (!enable) { connectErrorCountUpdater.incrementAndGet(this);
if (disableException != null) { throw disableException; }
throw new DataSourceDisableException(); } } catch (InterruptedException ie) { notEmpty.signal(); // propagate to non-interrupted thread notEmptySignalCount++; throw ie; } finally { notEmptyWaitThreadCount--; }
if (poolingCount == 0) { if (estimate > 0) { continue; }
waitNanosLocal.set(nanos - estimate); return null; } }
decrementPoolingCount(); DruidConnectionHolder last = connections[poolingCount]; connections[poolingCount] = null;
long waitNanos = nanos - estimate; last.setLastNotEmptyWaitNanos(waitNanos);
return last; } }
复制代码


用户头像

石小天

关注

还未添加个人签名 2018.11.07 加入

还未添加个人简介

评论

发布
暂无评论
Druid连接池源码阅读05_石小天_InfoQ写作社区