1

I have a standalone Java application that uses Hibernate to manage SQL connections. I work with a MySQL database and I ensure that all sessions are closed when the main() method exits.

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)

Does this mean that MySQL manually kills it? If so, how do I close my MySQL connection gracefully? 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.

EDIT: I am using C3P0 for this one - sorry forget to mention earlier. My connections do close but it's just that I get the [Warning] error on my mysql.err logs. I don't like seeing these types of warnings because clearly there's something wrong.

mpmp
  • 2,409
  • 4
  • 33
  • 46
  • 1
    possible duplicate of [Hibernate connections are not closed even with C3P0 + explicit session.close()](http://stackoverflow.com/questions/9755523/hibernate-connections-are-not-closed-even-with-c3p0-explicit-session-close) – Andreas Sep 05 '15 at 00:08
  • There is nothing in this post which would say he is using C3P0. – peterh Sep 05 '15 at 17:31
  • The following solution helped me. [enter link description here](http://stackoverflow.com/questions/40758569/all-used-connections-in-pool-are-sleepy-new-connections-are-created/40861706#40861706) – Rajas Gujarathi Nov 29 '16 at 09:10
  • Please see the link [All used connections in pool are sleepy & new connections are created](http://stackoverflow.com/questions/40758569/all-used-connections-in-pool-are-sleepy-new-connections-are-created/40861706#40861706) Hope it helps. It solved my problem. – Rajas Gujarathi Nov 29 '16 at 09:14

2 Answers2

0

Apparently, this is actually a bug from Hibernate.

I added another method that shuts down the C3P0ConnectionProvider in my HibernateUtil to be accessed statically as well.

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

After that, I never saw those pesky warnings again.

mpmp
  • 2,409
  • 4
  • 33
  • 46
-1

You control the Hibernate session in your code inside the run() method.

See javadoc of Session for an example. There are likely many other examples of Hibernate session control if you Google it.

If the call to HibernateUtil.closeCurrentSession() ever does something, then your run() method didn't correctly clean up after itself.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Control? What do you mean? Manually close the session? Because that's what `HibernateUtil.closeCurrentSession()` does. – mpmp Sep 04 '15 at 22:51
  • Since I don't know what your `run()` method is doing or how your HibernateUtil is coded, it's difficult to say what is going wrong here. Likely, you're *not* closing the database connection, so the connection is hard-aborted when the VM exits. – Andreas Sep 04 '15 at 23:50
  • HibernateUtil has a ThreadLocal object where I keep my session stored. The session object is put inside that ThreadLocal object on the first invocation of HibernateUtil.getCurrentSession() Everytime I HibernateUtil.getCurrentSession(), I check if there's an open session available, if not, I create one. What the run() method does is that it just queries and deletes entries from the database. HibernateUtil.closeCurrentSession() just closes the session bounded in the ThreadLocal object if it exists. In this case, how do I close the database connection manually? – mpmp Sep 05 '15 at 00:01