【发布时间】:2015-09-04 22:04:42
【问题描述】:
我有一个独立的 Java 应用程序,它使用 Hibernate 来管理 SQL 连接。我使用 MySQL 数据库,并确保在 main() 方法退出时关闭所有会话。
我的代码是这样的:
public static void main(String[] args) {
logger.info("Starting app");
App instance = new App();
try {
instance.run();
} catch (Exception e) {
logger.error("failed to complete run", e);
} finally {
HibernateUtil.closeCurrentSession();
}
logger.info("Finished run");
}
现在,每次这个 main 方法退出时,我都会在我的 sql 错误日志中看到一条日志:[Warning] Aborted connection 41533470 to db: 'user' user: 'some_user' host: 'some_ip_address_here' (Got an error reading communication packets)
这是否意味着 MySQL 手动杀死它?如果是这样,我如何优雅地关闭我的 MySQL 连接?如果我能正确回忆起我的 Hibernate,关闭会话只是将它们放回会话池中......我不确定如何完全告诉 Hibernate 释放它与 MySQL 的连接。
编辑:我为此使用 C3P0 - 抱歉忘记提早了。我的连接确实关闭了,但只是我的 mysql.err 日志上出现 [Warning] 错误。我不喜欢看到这些类型的警告,因为显然有问题。
【问题讨论】:
-
这篇文章中没有任何内容表明他正在使用 C3P0。
-
以下解决方案帮助了我。 enter link description here
-
请查看链接All used connections in pool are sleepy & new connections are created 希望对您有所帮助。它解决了我的问题。