Hibernate session.close()不返回连接到池 [英] Hibernate session.close() not returning connection to pool

查看:309
本文介绍了Hibernate session.close()不返回连接到池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My application has long running transactions and hence I tried the option session.close() at the end of every method to ensure that the connection objects are not held indefinitely for long time.

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

When session.close() option is used, I could see that the Hibernate's session object and the corresponding Connection object obtained from session.connection() are destroyed properly. But the issue is with the connection pool. The connection obtained by the session is not released back to the connection pool even after closing the session. Other requests are found waiting for connection from the pool.

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

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.

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

Has anyone faced this issue? Please let me know what am I missing here.

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

Follow-up: This is my hibernate configuration file details:

<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数据库的应用层使用JSF和EJB 2.1。 after_statement 似乎没有释放与池的连接。如果你需要更多的细节,请让我知道。

We use JSF and EJB 2.1 at the application layer connecting to Oracle DB. The after_statement doesn't seem to release the connection to the pool. Please let me know if u need any more details.

推荐答案


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

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 可能不如预期的那样(参见这里 这里)。

Could you try to define the hibernate.connection.release_mode property to after_statement explicitly? I know this is supposed to be the default but, depending on your context (could you be using Spring?), auto might not behave as expected (see here and here).

为了参考,这里是表3.4。 Hibernate JDBC和连接属性关于属性 hibernate.connection.release_mode

For reference, here is what the Table 3.4. Hibernate JDBC and Connection Properties writes about the property hibernate.connection.release_mode:


指定何时Hibernate应
释放JDBC连接。默认情况下,
一直保持JDBC连接,直到
会话显式关闭或
断开连接。 对于应用程序
服务器JTA数据源,在每个JDBC
调用后,使用
after_statement 来积极地
发布连接。
对于非JTA连接,
通常在每个
事务结束时释放
连接,通过使用
after_transaction 。对于JTA
和CMT交易策略,

选择 after_statement after_transaction 为JDBC
交易策略。

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.

eg auto (default)| on_close | after_transaction |
after_statement

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

此设置仅影响从
$ $返回的会话
C $ C> SessionFactory.openSession 。
通过
SessionFactory.getCurrentSession 获得的会话
CurrentSessionContext 实现
配置为使用控制
会话的
连接释放模式。请参阅第2.5节,
上下文会话

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 ?),你如何获得会话等。

If it doesn't help, please add more details about your environment and configuration (Spring?), how you get the session, etc.

这篇关于Hibernate session.close()不返回连接到池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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