我在我们的网络应用程序中使用 HikariCP,突然间,我遇到了这个异常:
[Hikari housekeeper (pool HikariPool-0)] WARN com.zaxxer.hikari.pool.ProxyLeakTask - Connection leak detection triggered for connection com.mysql.cj.jdbc.ConnectionImpl@3b82d04f, stack trace follows
java.lang.Exception:检测到明显的连接泄漏
我已经检查并确保我的所有 sql 代码都调用了 close 方法。
这是我的 HikariConfig:
private HikariConfig hikariConfig() {
HikariConfig config = new HikariConfig();
config.setDriverClassName(CLASS_FOR_NAME);
config.setJdbcUrl(HOST);
config.setUsername(USER);
config.setPassword(PASS);
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(30));
config.setValidationTimeout(TimeUnit.MINUTES.toMillis(1));
config.setMaximumPoolSize(40);
config.setMinimumIdle(0);
config.setMaxLifetime(TimeUnit.MINUTES.toMillis(2)); // 120 seconds max life time
config.setIdleTimeout(TimeUnit.MINUTES.toMillis(1)); // minutes
config.setConnectionTimeout(TimeUnit.MINUTES.toMillis(5)); // millis
config.setConnectionTestQuery("/* ping */ SELECT 1");
return config;
}
这是我的查询的样子:
public ArrayList<LocationType> getLocationTypes() {
ArrayList<LocationType> locationTypes = new ArrayList<>();
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try {
connection = DataSource.getInstance().getConnection();
pstmt = connection.prepareStatement("SELECT\n" +
" location_type_id,\n" +
" location_type_name\n" +
"FROM tablename;");
resultSet = pstmt.executeQuery();
while (resultSet.next()) {
locationTypes.add(new LocationType(resultSet.getInt("location_type_id"), resultSet.getString("location_type_name")));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (resultSet != null)
try {
resultSet.close();
} catch (SQLException e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (SQLException e) {
}
if (connection != null)
try {
connection.close();
} catch (SQLException e) {
}
}
return locationTypes;
}
我已经尝试增加connectionLeakDetection、最大连接和最小空闲,但都没有解决问题。
我读到这可能是由机器(资源不足)和连接关闭引起的,但我认为这些都不会导致问题。
我注意到我的代码中的一些长查询现在被检测为连接泄漏,即使我没有调用它们的方法。希望大家能够帮忙。
这是堆栈跟踪:
at com..database.DataSource.getConnection(DataSource.java:148)
at com.database.databaseServices.MileageReportService.MileageService.getMonthlySummaryBySID(MileageService.java:27)
at com.views.reports.mileage.MileageReport.lambda$generateReport$61446b05$1(MileageReport.java:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
这一行中唯一的一行 - MileageService.java:27 是我对数据库的查询,它在最终调用中有一个 close 语句。
最佳答案
升级到最新的 HikariCP。
HikariCP 将连接报告为泄漏,因为从借用到关闭所需的时间长于指定的 LeakDetectionThreshold。
版本 2.4.9 及更高版本,它会记录连接在报告泄漏后是否返回。
此外,查看您的配置,我会按如下方式调整属性 (前 4 到 1 分钟,最后到 5 分钟):
config.setLeakDetectionThreshold(TimeUnit.MINUTES.toMillis(1));
config.setConnectionTimeout(TimeUnit.MINUTES.toMillis(1));
config.setValidationTimeout(TimeUnit.MINUTES.toMillis(1));
config.setIdleTimeout(TimeUnit.MINUTES.toMillis(1));
config.setMaxLifetime(TimeUnit.MINUTES.toMillis(5));
HTH
关于java - 即使我的所有代码都调用 close 方法,HikariCP 也会检测到连接泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39996484/