DruidDataSource类源码分析(二)

分析DruidDataSource类中的方法getConnectionDirect( )

首先通过一个for循环自旋,确保连接的创建。

for (;;) {
    // handle notFullTimeoutRetry
    DruidPooledConnection poolableConnection;
    try {
        //调⽤getConnectionInternal 获取连接
        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

if (testOnBorrow) { // 默认是false
  	// 通过validationQuery测试连接是否正常
    boolean validate = testConnectionInternal(poolableConnection.holder, poolableConnection.conn);
    if (!validate) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("skip not validate connection.");
        }
      
				// ?
        discardConnection(poolableConnection.holder);
        continue;
    }
} else {
 			// 检查连接是否关闭
 			if (poolableConnection.conn.isClosed()) {
        		discardConnection(poolableConnection.holder); // 传入null,避免重复关闭
        		continue;
      }

testWhileIdle

  		if (testWhileIdle) {
        。。。
          // 如果连接空闲时间大于设置值,就测试validationQuery,验证连接是否有效
          if (idleMillis >= timeBetweenEvictionRunsMillis
              || idleMillis < 0 // unexcepted branch
             ) {
            boolean validate = testConnectionInternal(poolableConnection.holder, poolableConnection.conn);
            if (!validate) {
              if (LOG.isDebugEnabled()) {
                LOG.debug("skip not validate connection.");
              }

              discardConnection(poolableConnection.holder);
              continue;
            }
          }
        
        。。。
        
      }
  
  
}

检测过程都会调用 testConnectionInternal(poolableConnection.holder, poolableConnection.conn) 进行测试。

removeAbandoned

如果有线程从Druid中获取到了连接并没有及时归还,那么Druid就会定期检测该连接是否会处于运⾏状态,如果不处于运⾏状态,则被获取时间超过removeAbandonedTimeoutMillis就会强制回收该连接。

if (removeAbandoned) {
    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    poolableConnection.connectStackTrace = stackTrace;
    poolableConnection.setConnectedTimeNano();
    poolableConnection.traceEnable = true;

    activeConnectionLock.lock();
    try {
        activeConnections.put(poolableConnection, PRESENT);
    } finally {
        activeConnectionLock.unlock();
    }
}

defaultAutoCommit

最后设置是否自动commit,并返回通过poolableConnection = getConnectionInternal(maxWaitMillis)创建的连接

if (!this.defaultAutoCommit) {
    poolableConnection.setAutoCommit(false);
}

return poolableConnection;

问题

  1. 参数(testOnBorrow…)在哪里设置的?
  2. testConnectionInternal()怎么进行连接验证?
  3. discardConnection()有什么作用?
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值