Poison

testOnBorrow

最近有一组应用后端的 MySQL 实例 QPS 达到了 3w 左右,CPU 使用率在 60% 左右,今天有空简单看了下,其中 SELECT 1 语句竟然占用了接近一半的查询。经过排查,发现该组应用使用的连接池为 DBCP,且 testOnBorrow 属性使用的默认配置,即 true,其解释如下:

The indication of whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another.

即每次从池中拿到连接之前都对连接进行验证,猜测是该属性导致接近一半的 QPS 用于 SELECT 1 探测连接可用性。查询相关文档后将连接探测调整为空闲时进行,而不是每次从池中获取连接后都验证。

2022-01-13

调整完配置上线后通过监控数据观察到 QPS 几乎下降了一半,CPU 使用率也比之前降低了 20% 左右,效果还是比较明显的。

Reference

DBCP – BasicDataSource Configuration
Tomcat JDBC Connection Pool: testOnBorrow vs testWhileIdle - Stack Overflow
DruidDataSource配置 · alibaba/druid Wiki · GitHub
常见问题 · alibaba/druid Wiki · GitHub