Hibernation connections do not close even with C3P0 + explicit session.close ()

Sleeping connections to MySQL my db do not close. After clicking 10 times in 10 seconds, I get this connection statistics from MySQL Workbench (on my development machine. I am the only user). MySQL Workbench Server Status

I have those in place

  • C3P0 and it works (checked using log4j, there are no problems related to C3P0 and it seems to be running)
  • ServletReqestListener, which checks for an open session and closes it in the requestDestroyed () method.
  • The Hibernate session object is stored in ThreadLocal, so each request has only one connection, which opens on the first request and closes in the ServletRequestListener.
  • Each time I open a session and close the session, I output "Session Opened" and "Session Closed" to System.out, as in the code example. With every request, updating each page, I get "Session open" and after "Session closed", respectfully . So my little logic is working. But the connection does not close.

My hibernate.cfg.xml

<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">officenic</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/officenic</property>
<property name="hibernate.connection.username">officenic</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

<!-- configuration pool via c3p0 -->
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">100</property> <!-- seconds -->
<property name="hibernate.c3p0.max_size">5</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">100</property> <!-- seconds -->

The code block that I call every time I want to close the session.

if (session == null)
    return;

if (session.isOpen()) {

      if (session.isDirty())
         session.flush();

    session.close();
    System.out.println("Session closed");
}

Did I miss something?

+5
source share
2 answers
private static final ThreadLocal<Session> session = new ThreadLocal<Session>();

public static void closeSession() throws HibernateException {
    Session s = session.get();
    if (s != null) {
        s.close();
        session.remove();
    }
}

+1

All Articles