Hibernate session.close() 不返回连接到池

标签 hibernate session ejb jta

我的应用程序有长时间运行的事务,因此我在每个方法末尾尝试了选项 session.close() 以确保连接对象不会无限期地长时间保留。

当使用session.close()选项时,我可以看到Hibernate的session对象和从session.connection()获得的相应Connection对象被正确销毁。但问题出在连接池上。即使关闭 session 后, session 获得的连接也不会释放回连接池。发现其他请求正在等待池中的连接。

我在我的应用程序中使用 JTA 事务。在 hibernate.cfg.xml 中,我将 connection.release_mode 设置为 auto (默认),将 connection.autocommit 设置为 true。

有人遇到过这个问题吗?请让我知道我在这里缺少什么。

后续:这是我的 hibernate 配置文件详细信息:

<property name="connection.datasource">MXoraDS</property> 
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> 
<property name="connection.release_mode">after_statement</property> 
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property> 
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.JTASessionContext</property> 
<property name="transaction.auto_close_session">true</property> 
<property name="max_fetch_depth">2</property>

我们在连接 Oracle DB 的应用层使用 JSF 和 EJB 2.1。 after_statement 似乎没有释放到池的连接。如果您需要更多详细信息,请告诉我。

最佳答案

I am using JTA transaction in my application. In hibernate.cfg.xml, I have set connection.release_mode to auto (default) and connection.autocommit to true.

您能否尝试将 hibernate.connection.release_mode 属性显式定义为 after_statement ?我知道这应该是默认设置,但是根据您的上下文(您可以使用 Spring 吗?),auto 可能不会按预期运行(请参阅 here here)。

作为引用,以下是 Table 3.4. Hibernate JDBC and Connection Properties 的内容写了关于属性 hibernate.connection.release_mode:

Specifies when Hibernate should release JDBC connections. By default, a JDBC connection is held until the session is explicitly closed or disconnected. For an application server JTA datasource, use after_statement to aggressively release connections after every JDBC call. For a non-JTA connection, it often makes sense to release the connection at the end of each transaction, by using after_transaction. auto will choose after_statement for the JTA and CMT transaction strategies and after_transaction for the JDBC transaction strategy.

e.g. auto (default) | on_close | after_transaction | after_statement

This setting only affects Sessions returned from SessionFactory.openSession. For Sessions obtained through SessionFactory.getCurrentSession, the CurrentSessionContext implementation configured for use controls the connection release mode for those Sessions. See Section 2.5, “Contextual sessions”

如果没有帮助,请添加有关您的环境和配置(Spring?)、如何获取 session 等的更多详细信息。

关于Hibernate session.close() 不返回连接到池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33606715/

相关文章:

java - 如何根据 Java 对象的两个或多个字段为 hibernate 生成唯一的 Id?

php - session 变量与 Mysql 表

session - 无法加载 Spyder 保存的 session ?

java - EJB 和现代 Java 开发

java - 通过 com.sun.proxy 实例进行调用

java - EJB中无接口(interface) View 和接口(interface) View 的区别

hibernate - 是否可以在纯 JPA 中进行类型转换(从 boolean 到 yes_no)?

hibernate - 多个命名实体图可以在一个 JPQL 查询中传递吗?

java - 在 Grails 中使用 Hibernate Annotated Java 类时的日期问题

java - session 超时后如何重定向到指定页面