我正在开发一个运行在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()
但它还是告诉我连接是打开的。
我该怎么解决这个问题?谢谢!
发布于 2016-08-09 01:50:34
稍后调用preparedStatement.getConnection.close()只会得到一个不同的连接并关闭它。
你不能用这种模式来做你想做的事。调用this.dataSource.getConnection().prepareStatement(...)是从池中获取一个连接并丢弃对它的引用,之后没有人可以关闭它。即使保留了对它的引用,也不能在此方法中关闭连接,因为返回的PreparedStatement将不可用。
https://stackoverflow.com/questions/38831963
复制相似问题