-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Description
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:
- Set the database connection pool (Commons DBCP) size to 1 with a 5 second timeout waiting for an available connection
- 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.
- 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:
- Database connection problems cause Spring to raise an IllegalStateException and resulting in transaction state not being cleaned up [SPR-5133] #9806 Database connection problems cause Spring to raise an IllegalStateException and resulting in transaction state not being cleaned up