HikariPool 频繁点击查询连接池提示超时

经过一些SQL查询后,系统无法从HikariPool获得任何连接,并提示以下错误消息

HikariPool-1 - Connection is not available, request timed out after 30002ms.

经过测试

  • HikariCP 3.3.1
  • MySQL的连接器的Java 5.1.47
  • Java 8

1.解决方案

1.1启用com.zaxxer.hikari的调试日志,它将打印出许多有用的信息。

logback.xml

	
        
    
Terminal
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........................
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默认情况下, connectionTimeout30秒。

HikariPool-1 - Connection is not available, request timed out after 30001ms.

1.3这意味着Hikari池达到最大连接total=10, active=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;
        }

    }

参考文献

  • 光ikaCP
  • 连接不可用,请求在30002ms后超时

翻译自: https://mkyong.com/jdbc/hikaripool-1-connection-is-not-available-request-timed-out-after-30002ms/

你可能感兴趣的:(java,mysql)