即使使用C3P0 +显式session.close(),Hibernate连接也不会关闭

Ser*_*rin 5 java connection hibernate connection-pooling connection-close

与我的数据库的Hibernate连接我的数据库没有关闭.在10秒内点击10次之后,我从MySQL Workbench获得了这个连接统计数据(在我的开发机器中.我是唯一的用户).MySQL Workbench服务器状态

我有这些

  • C3P0并运行(从log4j检查,没有与C3P0相关的问题,似乎正在运行)
  • 一个ServletReqestListener,用于检查是否存在打开的会话,并在requestDestroyed()方法中将其关闭.
  • Hibernate Session对象保存在ThreadLocal中,因此每个请求只有一个连接,在第一次查询时打开,并在ServletRequestListener中关闭.
  • 每次打开会话并关闭会话时,我都会将"Session Opened"和"Session Closed"输出到System.out,如代码示例中所示.在每个请求,每个页面刷新,我得到"会话打开"和"会话结束",分别.所以我的小逻辑有效.但是连接没有关闭.

我的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 -->
Run Code Online (Sandbox Code Playgroud)

我想在每次关闭会话时调用的代码块.

if (session == null)
    return;

if (session.isOpen()) {

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

    session.close();
    System.out.println("Session closed");
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

Ser*_*rin 5

好吧,我似乎每次都在创建SessionFactory.这个链接有一个很好的类,使SessionFactory静态解决了这个问题.http://docs.jboss.org/hibernate/core/3.3/reference/en/html/tutorial.html#tutorial-firstapp-helpers