HikariPool LeakDetectionThreshold 检测到连接泄漏

Connection leak detected by HikariPool LeakDetectionThreshold

我有 Hikari 连接池,配置如下

    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setJdbcUrl(dbProps.getUrl());
    hikariConfig.setUsername(dbProps.getUser());
    hikariConfig.setPassword(dbProps.getPass());
    hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
    hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
    hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
    hikariConfig.setMaximumPoolSize(Runtime.getRuntime().availableProcessors() * 5 + 2);
    hikariConfig.setIdleTimeout(36000);
    hikariConfig.setConnectionTestQuery("SELECT 1;");
    hikariConfig.setConnectionTimeout(20000);
    hikariConfig.setMaxLifetime(1440000);
    hikariConfig.setLeakDetectionThreshold(3000); // LEAK DETECTION THRESHOLD
    dataSource = new HikariDataSource(hikariConfig);

一些数据库的详细信息如下:

public class SomeDBFetcher {

private DataSource dataSource;

public SomeDBFetcher(final DataSource dataSource) {
    this.dataSource = dataSource;
}

public void someMethod() {
    try (final var conn = dataSource.getConnection(); // CONNECTION LEAKS ?
        final var stmt = conn.prepareStatement("SOME QUERY")) {
        // SOME DB
    }
}

}

在日志中,我看到连接泄漏。为什么会发生连接泄漏?

LeakDetectionThreshold 当您没有在定义的时间限制内关闭连接时通知潜在泄漏 - 在您的情况下是 3 秒。

您的查询(或 try 子句中的某些内容)可能需要超过 3 秒,并且 Hikari 会警告您潜在的连接泄漏。

因此,如果您看到这样的警告,并不一定意味着您确实有泄漏。

来自文档:

leakDetectionThreshold
This property controls the amount of time that a connection can be out of the pool before a message is logged indicating a possible connection leak. A value of 0 means leak detection is disabled. Lowest acceptable value for enabling leak detection is 2000 (2 seconds). Default: 0

https://github.com/brettwooldridge/HikariCP