DB driuid连接池 removeAbandanded 配置导致CPU使用率飙高
单机压测发现请求QPS不高也很吃CPU,通过观察火焰图和热点代码发现是获取DB连接池的问题。
DB连接池底层使用了driuid,并且将removeAbandanded默认配置为true。
在com.alibaba.druid.pool.DruidDataSource#getConnectionDirect
中有一段逻辑
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();
}
}
Thread.currentThread().getStackTrace();
这一行代码比较吃CPU。
具体请参考 连接泄漏监测,该参数在线上不应该不配置为true,因为虽然MySQL有连接8小时空闲就断开的feature,但是线上只要连接池配置的合理,一般不会出现此问题。据他人测试,设置该参数为false,获取连接的性能可以提高10倍。