Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate connections are not closed even with C3P0 + explicit session.close()

Hibernate connections to MySQL my db are not closing. After clicking 10 times in like 10 second, I get this connection statistics from MySQL Workbench (in my development machine. I'm the only user).MySQL Workbench Server Status

I have those in place

  • C3P0 and running (checked from log4j, no problem related to C3P0 and seems running)
  • A ServletReqestListener which checks if there's an open session and closes it in requestDestroyed() method.
  • Hibernate Session object is being kept in ThreadLocal, so every request only have one connection, which opens at first query, and closes in ServletRequestListener.
  • Every time I open a session and close a session I output "Session Opened" and "Session Closed" to System.out as in the code example blow. At every request, every page refresh, I get "Session Opened" and after "Session Closed", repectively. So my little logic works. But the connection does not get closed.

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 I call in everytime where 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");
}

Do I miss something?

like image 496
Seregwethrin Avatar asked Mar 18 '12 02:03

Seregwethrin


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

actually I am doing like this and it works

like image 24
Nurlan Avatar answered Sep 28 '22 23:09

Nurlan


Well it seems I was creating SessionFactory everytime. There's a nice class here at the link, making SessionFactory static solved the problem. http://docs.jboss.org/hibernate/core/3.3/reference/en/html/tutorial.html#tutorial-firstapp-helpers

like image 83
Seregwethrin Avatar answered Sep 28 '22 22:09

Seregwethrin