【发布时间】:2016-08-09 09:50:34
【问题描述】:
我正在开发一个在 Java 服务器上运行的游戏。对于数据库池,我使用的是 HikariCP,它是一个优秀的库,但它现在以我的方式抛出以下错误:
[Hikari housekeeper (pool HikariPool-0)] WARN com.zaxxer.hikari.pool.ProxyLeakTask - Connection leak detection triggered for connection com.mysql.jdbc.JDBC4Connection@5910e440, stack trace follows
java.lang.Exception: Apparent connection leak detected
at nl.finicky.lemonade.database.Database.prepare(Database.java:43)
at nl.finicky.lemonade.rooms.RoomFactory.loadRoom(RoomFactory.java:25)
at nl.finicky.lemonade.rooms.RoomFactory.<init>(RoomFactory.java:20)
at nl.finicky.lemonade.Lemonade.main(Lemonade.java:30)
现在我知道连接泄漏意味着打开的连接在某处浮动,但我不知道如何或在哪里。
以下是我的数据库类中的一个方法,根据堆栈跟踪应该会出现错误。
public PreparedStatement prepare(String query) {
PreparedStatement statement = null;
try {
while (statement == null) {
statement = this.dataSource.getConnection().prepareStatement(query, 1);
// The line triggering the error ^
if (statement != null) {
this.databasePool.getStorage();
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
return statement;
}
}
这只是启动语句的基本方法。当这个被调用时,我使用它然后调用resultSet.close()、preparedStatement.getConnection.close()和preparedStatement.close()
它仍然告诉我连接已打开。
我该如何解决这个问题?谢谢!
【问题讨论】: