今天详细梳理下Druid获取连接的过程:
- 首先调用getConnectionInternal方法尝试获取池化连接,在获取超时的情况下(捕获到
GetConnectionTimeoutException),如果重试次数小于notFullTimeoutRetryCount并且this.poolingCount + this.activeCount >= this.maxActive,会重新尝试获取连接:
// handle notFullTimeoutRetry DruidPooledConnection poolableConnection; try { poolableConnection = getConnectionInternal(maxWaitMillis); } catch (GetConnectionTimeoutException ex) { if (notFullTimeoutRetryCnt <= this.notFullTimeoutRetryCount && !isFull()) { notFullTimeoutRetryCnt++; if (LOG.isWarnEnabled()) { LOG.warn("get connection timeout retry : " + notFullTimeoutRetryCnt); } continue; } throw ex; }
- 如果testOnBorrow为true,则为验证第一步获取到的连接是否可用,如果连接不可用会丢弃该连接并重新回到第一步获取连接:
if (testOnBorrow) { boolean validate = testConnectionInternal(poolableConnection