我有一个使用Hibernate来管理SQL连接的独立Java应用程序。我使用MySQL数据库,并确保当main()方法退出时所有会话都关闭。Hibernate/C3P0没有正确关闭MySQL连接(mysql.err上的异常连接)
我的代码是这样的:
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");
}
现在,每当这个主要方法退出,我看到一个登录我的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]错误。我不喜欢看到这些类型的警告,因为显然有错误。
[Hibernate连接可能重复,即使使用C3P0 +显式session.close()也不会关闭(http://stackoverflow.com/questions/9755523/hibernate-connections-are-not-closed-even-with- c3p0-explicit-session-close) – Andreas
这篇文章中没有任何内容会说他正在使用C3P0。 – peterh
以下解决方案对我有所帮助。 [在此输入链接描述](http://stackoverflow.com/questions/40758569/all-used-connections-in-pool-are-sleepy-new-connections-are-created/40861706#40861706) – Rajas