简体   繁体   English

Hibernate / C3P0无法正确关闭MySQL连接(mysql.err上的异常终止连接)

[英]Hibernate/C3P0 not closing MySQL connections properly (Aborted connections on mysql.err)

I have a standalone Java application that uses Hibernate to manage SQL connections. 我有一个使用Hibernate来管理SQL连接的独立Java应用程序。 I work with a MySQL database and I ensure that all sessions are closed when the main() method exits. 我使用MySQL数据库,并确保在main()方法退出时关闭所有会话。

My code goes something like this: 我的代码是这样的:

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");
}

Right now, everytime this main method exits, I see a log in my sql error logs saying: [Warning] Aborted connection 41533470 to db: 'user' user: 'some_user' host: 'some_ip_address_here' (Got an error reading communication packets) 现在,每当这个主要方法退出时,我都会在sql错误日志中看到一条日志: [Warning] Aborted connection 41533470 to db: 'user' user: 'some_user' host: 'some_ip_address_here' (Got an error reading communication packets)

Does this mean that MySQL manually kills it? 这是否意味着MySQL手动将其杀死? If so, how do I close my MySQL connection gracefully? 如果是这样,如何正常关闭MySQL连接? If I can recall my Hibernate correctly, closing sessions just put them back into the pool of sessions... I'm not sure how to entirely tell Hibernate to release its connection with MySQL. 如果我可以正确地回忆起我的Hibernate,则关闭会话只是将它们放回会话池中……我不确定如何完全告诉Hibernate释放其与MySQL的连接。

EDIT: I am using C3P0 for this one - sorry forget to mention earlier. 编辑:我正在为此使用C3P0-对不起,忘记提起。 My connections do close but it's just that I get the [Warning] error on my mysql.err logs. 我的连接确实关闭了,但是仅仅是我的mysql.err日志上出现了[警告]错误。 I don't like seeing these types of warnings because clearly there's something wrong. 我不喜欢看到这些类型的警告,因为显然存在问题。

Apparently, this is actually a bug from Hibernate. 显然,这实际上是Hibernate的一个错误。

I added another method that shuts down the C3P0ConnectionProvider in my HibernateUtil to be accessed statically as well. 我添加了另一个方法,该方法也可以关闭HibernateUtil中的C3P0ConnectionProvider以进行静态访问。

SessionFactoryImpl impl = (SessionFactoryImpl) sessionFactory;
C3P0ConnectionProvider provider = (C3P0ConnectionProvider) impl.getConnectionProvider();
provider.close();

After that, I never saw those pesky warnings again. 在那之后,我再也没有看到那些讨厌的警告。

You control the Hibernate session in your code inside the run() method. 您可以在代码中的run()方法中控制Hibernate会话。

See javadoc of Session for an example. 有关示例,请参见Session javadoc。 There are likely many other examples of Hibernate session control if you Google it. 如果您使用Google,Hibernate会话控件可能还有许多其他示例。

If the call to HibernateUtil.closeCurrentSession() ever does something, then your run() method didn't correctly clean up after itself. 如果对HibernateUtil.closeCurrentSession()的调用曾经做过任何事情,则您的run()方法在run()完之后无法正确清除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM