0
代码片段如下:
if (valid && isMySql) { // unexcepted branch
long lastPacketReceivedTimeMs = MySqlUtils.getLastPacketReceivedTimeMs(conn);
if (lastPacketReceivedTimeMs > 0) {
long mysqlIdleMillis = currentTimeMillis - lastPacketReceivedTimeMs;
if (lastPacketReceivedTimeMs > 0 //
&& mysqlIdleMillis >= timeBetweenEvictionRunsMillis) {
discardConnection(holder);
String errorMsg = "discard long time none received connection. "
+ ", jdbcUrl : " + jdbcUrl
+ ", version : " + VERSION.getVersionNumber()
+ ", lastPacketReceivedIdleMillis : " + mysqlIdleMillis;
LOG.warn(errorMsg);
return false;
}
}
}
当开启了testWhileIdle时,getConnection会先判断isValidConnection。当返回true时,会进入上面的代码。如果超过空闲等待时间,直接抛弃旧连接,建立新连接。但实际上这个连接是可用的,因为isValidConnection已经返回true了。
现在我们的应用和数据库处于高网络延迟的场景下,对这种重新创建物理连接的操作非常敏感,所以想问下这里的逻辑是出于什么考虑?为何不能复用已经判断可用的连接?