Skip to content

HibernateTransactionManager doesn't clean up when connection not available [SPR-3693] #8374

@spring-projects-issues

Description

@spring-projects-issues

David Hay opened SPR-3693 and commented

If there is a problem opening a connection (e.g. thread pool at capacity and the thread times out waiting for a free database connection), it appears that the HibernateTransactionManager doesn't clean up the SessionHolder object correctly. We diagnosed this by doing the following:

  1. Set the database connection pool (Commons DBCP) size to 1 with a 5 second timeout waiting for an available connection
  2. Make a request that hits the database and use a debugger to stop that thread (thread 1) while the sole database connection is in use.
  3. Make another request that will create (at least) two separate database transactions. (thread 2)
    3a. When the first transaction begins in thread 2, an exception is thrown and caught by HibernateTransactionManager.doBegin(), which throws a CannotCreateTransactionException.
    3b. If the CannotCreateTransactionException is caught by the application and then another transaction is started, HibernateTransactionManager attempts to create a new Session, rather than use the session created by OpenSessionInViewFilter. If the connection is now available, the ConnectionHolder is bound with the data source in a thread local. However, the transaction again fails because of the following:

java.lang.IllegalStateException: Already value [org.springframework.orm.hibernate3.SessionHolder@78d8ac] for key [org.hibernate.impl.SessionFactoryImpl@1ccf137] bound to thread [http-8080-Processor2]

This leaves the ConnectionHolder bound to the thread local after the request finishes.

Now, any future requests on that thread fail with the following exception because the ConnectionHolder is still bound to the thread local:

org.springframework.transaction.IllegalTransactionStateException: Pre-bound JDBC Connection found! HibernateTransactionManager does not support running within DataSourceTransactionManager if told to manage the DataSource itself. It is recommended to use a single HibernateTransactionManager for all transactions on a single DataSource, no matter whether Hibernate or JDBC access.


Affects: 2.0.5, 2.0.6, 2.1 M1, 2.1 M2

Issue Links:

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions