gpt4 book ai didi

java - HikariPool LeakDetectionThreshold 检测到连接泄漏

转载 作者:行者123 更新时间:2023-12-02 01:02:54 31 4
gpt4 key购买 nike

我有 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

关于java - HikariPool LeakDetectionThreshold 检测到连接泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60424898/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com