Hibernate session.close() not returning connection to pool

后端 未结 2 1288
梦如初夏
梦如初夏 2020-12-09 18:31

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 indef

相关标签:
2条回答
  • 2020-12-09 18:48

    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.

    0 讨论(0)
  • 2020-12-09 19:02

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

    0 讨论(0)
提交回复
热议问题