即使使用 C3P0 + 显式 session.close() 也不会关闭休眠连接 [英] Hibernate connections are not closed even with C3P0 + explicit session.close()

查看:51
本文介绍了即使使用 C3P0 + 显式 session.close() 也不会关闭休眠连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与我的数据库的 MySQL 的休眠连接没有关闭.在 10 秒内单击 10 次后,我从 MySQL Workbench (在我的开发机器中.我是唯一的用户)获取此连接统计信息.MySQL Workbench 服务器状态

我已经准备好了

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 并正在运行(从 log4j 中检查,与 C3P0 相关的问题没有并且似乎正在运行)
  • 一个 ServletReqestListener 检查是否有一个打开的会话并在 requestDestroyed() 方法中关闭它.
  • Hibernate Session 对象保存在 ThreadLocal 中,因此每个请求只有一个连接,该连接在第一次查询时打开,并在 ServletRequestListener 中关闭.
  • 每次打开会话并关闭会话时,我都会向 System.out 输出会话已打开"和会话已关闭",如代码示例中所示.在每次请求、每次页面刷新时,我分别得到会话打开"和会话关闭"之后.所以我的小逻辑有效.但连接没有关闭.
  • 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.

我的 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 -->

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

if (session == null)
    return;

if (session.isOpen()) {

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

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

我错过了什么吗?

推荐答案

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

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

这篇关于即使使用 C3P0 + 显式 session.close() 也不会关闭休眠连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆