Search code examples
javahikaricp

Connection leak detected by HikariPool LeakDetectionThreshold


I have Hikari Connection Pool, And configured as below

    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);

And Some DB details is fetched as below :

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
    }
}

}

In the logs , I am seeing connection leaks. Why connection leaks is happening?


Solution

  • LeakDetectionThreshold informs about potential leaks when you don't close your connection in defined limit of time - in your case it's 3s.

    Here probably you have query (or something inside try clause) that takes longer than 3 seconds, and Hikari warns you about potential connection leak.

    So if you see such a warning, it doesn't necessarily mean you actually have a leak.

    From documentation:

    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