Sep 6th, 2008, 12:18 AM
1.2->2.5 migration: Hibernate Transactions
I spent 14 hours today at work and couldn't solve this problem. If anyone can provide assistance, please help.
I am converting a project with about 700 classes from Spring 1.2.9 to 2.5.5. Here are some details:
* Hibernate 3.2.6
* All DAO implementations will continue to use HibernateDaoSupport
* HibernateSession is injected to the HibernateDaoSupport subclasses
* We replaced any custom HibernateCallbacks with invocations to getSession(false)
* <tx:annotation-driven /> wraps all our services objects
* <context:annotation-config /> autowires dependencies
We are using a Hibernate Interceptor to populate auditing information. Inside of its onSave(), it calls sessionFactory.getCurrentSession(). This is where the problem seems to be. Unlike the usage in Spring 1.2.9, in 2.5.5 it returns a new session. Our interceptor delves into the current Session to get some information about the Hibernate Entity's class. It used to getting the session bound to the Spring TX, but no longer. Something to do with Spring's Hibernate 3.1 support...
I know why the second session is returned, but I can't solve it correctly. The transaction synchronization manager is first keying against the Session Proxy, but the Interceptor's call to SessionFactory.getCurrentSession() is keying against the native Session. The synchronization keys don't match, so a new session gets created.
What to do? Currently, it's not a business option for me to eliminate our use of HibernateDaoSupport.
Two big issues to solve:
* Will getSession(false) in HibernateDaoSupport be a suitable emulation of the session passing to HibernateCallback?
* How can the Hibernate Interceptor get the same session used by the Spring TX?
Sep 6th, 2008, 09:38 AM
I appear to have solved it. I will have to do more full systems testing to be sure, but if I don't respond, that means the investigation is over. :-)
The Hibernate Interceptor needed to be using SessionFactoryUtils.getSession(sf, false). That will use the prebound Hibernate Session on the transaction. Using straight Hibernate code goes against the native Session, which created a new session -- not my intention.
Sep 8th, 2008, 04:59 AM
I expect some misconfiguration on hibernate. Post your configuration.
Apr 16th, 2009, 10:08 PM
LocalSessionFactoryBean.getCurrentSession() vs SessionFactoryUtils.getSession
Martin, I'm seeing the same problem as paul;
I posted my configuration to http://forum.springsource.org/showth...saction&page=2
before seeing that post was marked as "thumbs-up"... so maybe nobody will look there.
Could you take a look, and maybe comment on what kind of mis-configuration might cause LocalSessionFactoryBean to mis-communicate with HibernateTransactionManager?
Tags for this Thread