我的应用程序有长时间运行的事务,因此我在每个方法末尾尝试了选项 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 usingafter_transaction
.auto
will chooseafter_statement
for the JTA and CMT transaction strategies andafter_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 throughSessionFactory.getCurrentSession
, theCurrentSessionContext
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/