HikariPool-1 连接不可用 Connection is not available, request timed out after 30002ms. - zaxxer
一些SQL查询后,系统无法从HikariPool获取任何连接,并提示如下错误信息
HikariPool-1 - Connection is not available, request timed out after 30002ms.
经测试
光CP 3.3.1
mysql-connector-java 5.1.47
爪哇 8
1.解决方案
1.1 启用 com.zaxxer.hikari 的调试日志,它会打印出许多有用的信息。
logback.xml
<!-- this config is for logback framework -->
<logger name="com.zaxxer.hikari" level="debug" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
终端
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - HikariPool-1 - configuration:
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - allowPoolSuspension.............false
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - autoCommit......................true
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - catalog.........................none
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - connectionInitSql...............none 拼多多帮助力入口
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - connectionTestQuery.............none
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - connectionTimeout...............30000
//...
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - jdbcUrl.........................jdbc:mysql://192.168.1.4:3306/wordpress
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - leakDetectionThreshold..........0
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - maxLifetime.....................1800000
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - maximumPoolSize.................1
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - metricRegistry..................none
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - metricsTrackerFactory...........none
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - minimumIdle.....................1
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - password........................<masked>
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - poolName........................"HikariPool-1"
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - readOnly........................false
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - registerMbeans..................false
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - scheduledExecutor...............none
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - schema..........................none
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - threadFactory...................internal
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - transactionIsolation............default
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - username........................"mkyong"
2019-03-20 13:46:23 [main] DEBUG com.zaxxer.hikari.HikariConfig - validationTimeout...............5000
2019-03-20 13:47:25 [main] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Timeout failure stats (total=10, active=10, idle=0, waiting=0)
2019-03-20 13:47:25 [main] ERROR c.mkyong.calculator.StartApplication - [Exception] : HikariPool-1 - Connection is not available, request timed out after 30001ms.
1.2 默认情况下,connectionTimeout 为 30 秒。
HikariPool-1 - Connection is not available, request timed out after 30001ms.
1.3 这意味着 Hikari 池达到最大连接总数=10,活动=10
HikariPool-1 - Timeout failure stats (total=10, active=10, idle=0, waiting=0)
1.4 多为连接泄漏,一般是从池中借用后连接没有关闭造成的。
例如下面的代码,getConnection() 没有关闭,这会导致连接泄漏。
@Override
public void update(int postId, String metaValue) throws SQLException {
try (PreparedStatement ps =
dataSource.getConnection().prepareStatement(SQL_UPDATE_POST_META)) {
ps.setString(1, metaValue);
ps.setInt(2, postId);
ps.setString(3, GlobalUtils.META_KEY);
ps.executeUpdate();
} catch (SQLException e) {
logger.error("[UPDATE] [POST_ID] : {}, [META_VALUE] : {}", postId, metaValue);
throw e;
要修复它,只需关闭连接。
@Override
public void update(int postId, String metaValue) throws SQLException {
try (Connection connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(SQL_UPDATE_POST_META)) {
ps.setString(1, metaValue);
ps.setInt(2, postId);
ps.setString(3, GlobalUtils.META_KEY);
ps.executeUpdate();
} catch (SQLException e) {
logger.error("[UPDATE] [POST_ID] : {}, [META_VALUE] : {}", postId, metaValue);
throw e;