我在我们的网络应用程序中使用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,max connection和minimum idle,但没有一个解决了这个问题。
我已经读过它可能是由机器(资源不足)和连接关闭造成的,但我认为,这些都不会导致问题。
我注意到我的代码中的一些长查询现在被检测为连接泄漏,即使我没有调用他们的方法。我希望你们能提供帮助。
这是堆栈跟踪:
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,是我对DB的查询,它在finally调用中有一个close语句。
答案 0 :(得分:0)
升级到最新的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