Hibernate session.close()没有返回到池的连接

时间:2010-10-13 13:19:57

标签: hibernate session ejb jta

我的应用程序具有长时间运行的事务,因此我在每个方法的末尾尝试了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似乎没有释放与池的连接。如果您需要更多详细信息,请与我们联系。

2 个答案:

答案 0 :(得分:20)

  

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

您可以尝试明确地将hibernate.connection.release_mode属性定义为after_statement吗?我知道这应该是默认值,但是,根据您的上下文(您是否可以使用Spring?),auto可能无法按预期运行(请参阅here { {3}})。

作为参考,以下是here写有关属性hibernate.connection.release_mode

的内容
  

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

     

例如 auto(默认)| on_close | after_transaction |   after_statement

     

此设置仅影响会话   从...返回   SessionFactory.openSession。对于   通过获得的会话   SessionFactory.getCurrentSession,   CurrentSessionContext实施   配置使用控制   那些连接释放模式   会话。见Table 3.4. Hibernate JDBC and Connection Properties

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

答案 1 :(得分:0)

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

相关问题