【问题标题】:Hibernate Fails to Close Connection?Hibernate 无法关闭连接?
【发布时间】:2011-01-03 13:19:58
【问题描述】:

有没有出现hibernate无法关闭打开的连接???

我正在使用 Hibernate 3.2.5Oracle 10g express 开发一个 JSF 2.0 应用程序。经过固定数量的事务后,hibernate 可以连接到数据库,甚至我也无法使用 sqlDeveloper 连接。正如我所检查的,在此失败之前的事务数几乎等于 init.ora 中的 进程 的数量>.

我总是使用这样的休眠会话:

try {
      session.beginTransaction();

      ... 

    } catch (Exception ex) {
      Transaction tx = session.getTransaction();
      if (tx.isActive()) {
        tx.rollback();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, ex.getMessage(), ex.getMessage()));
      }
    } finally {
      session.close();
    }

我的休眠配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
    <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
    <property name="hibernate.connection.username">CUSTOMS_G2G</property>
    <property name="hibernate.connection.password">123456</property>
    <mapping resource="ir/khorasancustoms/g2g/persistance/UserGroup.hbm.xml"/>
    <mapping resource="ir/khorasancustoms/g2g/persistance/User.hbm.xml"/>
    <mapping resource="ir/khorasancustoms/g2g/persistance/CatalogGroup.hbm.xml"/>
    <mapping resource="ir/khorasancustoms/g2g/persistance/CatalogValue.hbm.xml"/>
    <mapping resource="ir/khorasancustoms/g2g/persistance/gates/Receipt.hbm.xml"/>
    <mapping resource="ir/khorasancustoms/g2g/persistance/gates/Price.hbm.xml"/>
    <mapping resource="ir/khorasancustoms/g2g/persistance/gates/Promise.hbm.xml"/>
    <mapping resource="ir/khorasancustoms/g2g/persistance/gates/Exit.hbm.xml"/>
    <mapping resource="ir/khorasancustoms/g2g/persistance/gates/Weight.hbm.xml"/>
    <mapping resource="ir/khorasancustoms/g2g/persistance/gates/Fee.hbm.xml"/>
    <mapping resource="ir/khorasancustoms/g2g/persistance/Page.hbm.xml"/>
    <mapping resource="ir/khorasancustoms/g2g/persistance/Permission.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

【问题讨论】:

    标签: hibernate jsf-2


    【解决方案1】:

    可能存在与您的 hibernate.connection.release_mode 相关的问题。看看here .

    【讨论】:

    • 我手动设置为 on_close 但问题依然存在。
    • after_transaction 呢?
    • 没有变化! :( 有没有办法让它使用 GlassFish 池?
    • 是的,有。看看这个链接。 wiki.tony-su.com/…
    • 谢谢,看来问题已经解决了;)(使用 GlassFish 池)
    【解决方案2】:

    我会为hibernate logging(特别是 org.hibernate.transactionorg.hibernate.jdbc)启用 DEBUG 并查看发生了什么。这种方式更容易,因为您不使用任何池,这是查找正在发生的事情的最简单方法

    【讨论】:

    • 你能更详细地解释一下如何启用它吗?顺便说一下,我使用的是 GlassFish 应用程序服务器,如何配置域并让 hibernate 从其池中获取连接?
    • 一次一件事。首先专注于确定连接没有被关闭的原因。您需要阅读有关如何在 slf4j 中启用日志记录的教程。 Hibernate 使用它来记录日志,所以一旦你知道如何为 slf4j 配置日志记录,那么你就很好了。在这里阅读slf4j.org/manual.html 大部分时间它只是在类路径中放置一个 xml 配置文件。
    【解决方案3】:

    在 .net 世界中,我在关闭之前检查会话是否为 not nullopen,因为有时我会看到此错误,不确定 JPA 是否也是如此,但请参阅下面的代码...

    例如

    if (session != null && session.IsOpen)
          session.close();
    

    另见post

    【讨论】:

    • 我调用session.close()的时候没有问题,但是好像根本没有关闭底层连接。
    猜你喜欢
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2017-02-17
    相关资源
    最近更新 更多