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

发布于 2024-09-27 03:26:01 字数 1326 浏览 3 评论 0 原文

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

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

我在我的应用程序中使用 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 似乎没有释放到池的连接。如果您需要更多详细信息,请告诉我。

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.

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.

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.

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>

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗳卜坏 2024-10-04 03:26:01

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

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

作为参考,以下是 表 3.4。 Hibernate JDBC 和连接属性 写了有关属性 hibernate.connection.release_mode 的内容:

指定 Hibernate 何时应该
释放 JDBC 连接。默认情况下,
一个 JDBC 连接会一直保持到
会话已明确关闭或
已断开连接。 对于申请
服务器JTA数据源,使用
after_statement 积极地
每次 JDBC 后释放连接
对于非 JTA 连接,它
释放通常是有意义的
每个末尾的连接
交易,通过使用
after_transaction自动
为 JTA 选择 after_statement
和 CMT 交易策略以及
JDBC 的 after_transaction
交易策略。

例如 自动(默认)| on_close | after_transaction |
after_statement

此设置仅影响会话
从返回
SessionFactory.openSession。为了
通过以下方式获得的会话
SessionFactory.getCurrentSession
CurrentSessionContext 实现
配置为使用控制
连接释放模式
会议。请参阅第 2.5 节,
“情境会话”

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

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.

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).

For reference, here is what the Table 3.4. Hibernate JDBC and Connection Properties writes about the property 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”

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

本宫微胖 2024-10-04 03:26:01

如果您使用 JDBCTransactionManager,则当事务结束时,连接将返回到连接池。

if you are using the JDBCTransactionManager, the connection will be returned to the connectionpool when the transactions ends.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文