Join GitHub today
Session not flushed with Hibernate 4.1.3, JTA and Spring transaction management integration [SPR-9404] #14040
I have been migrating from hibernate 3.3.x to hibernate 4.1.3-final using Spring 3.1.1-release.
As it turned out the migration when smooth and everything was working on a local tomcat.
So then learned that in case of JTA you have to specify manually how the tx manager/user transaction can be found.
hibernate.transaction.jta.platform and set it to SunOneJtaPlatform.
This resolved the "No Session found for current thread" as it initialized the jtaSessionContext with the txmanager provided by the configured JtaPlatform.
However, now it turns out that the hibernate session is not flushed before the transaction is commited and hence no modifications are written to database.
What is also bizarre is that in hibernate3 modus everything goes via the Spring TransactionSynchronizationManager (even in JTA mode).
All of this is gone with the hibernate4 integration from the moment a JTA environment is detected.
I supplied two samples:
Both samples have a POM so it should be trivial to build them.
5 votes, 4 watchers
Erwin Vervaet commented
Is there documentation available as to how and why the Spring Hibernate integration changed between Hibernate 3 and Hibernate 4?
From the comments of #12751: "Good news: There is a working prototype already, and we decided to promote Hibernate 4.0 support to the Spring 3.1 feature set now. It won't be a direct 1:1 match with the orm.hibernate3 package but will provide everything needed for doing things the Hibernate 4 way within a Spring-based application. For example, there won't be a HibernateTemplate anymore, and we rely on Hibernate's own JTA support for JTA synchronization now."
So I have the feeling that we're missing a bit of context/explanation to setup TX management and JTA properly with Spring 3.1 + Hibernate 4.
Erwin Vervaet commented
Seems like indeed we were missing a bit of Hibernate config. Adding the following to the Hibernate properties seems to resolve the problem:
So this might be a non-issue as far the Spring source code is concerned, but still important in that there really is a need for a decent explanation on how to migrate your Spring + Hibernate3 config to Spring + Hibernate4. Probably something for the Spring documentation.
Alysson Rodrigues commented
Instead of bootstrapping the entity manager factory using Spring LocalContainerEntityManagerFactory I have set JBoss AS 7.1 to bootstrap it (declaring a persistence.xml) and get it registered in JNDI, then Spring gets it from there. As soon as I have set it, everything works as expected.
Here is the persistence unit:
<persistence-unit name="persistenceUnit" transaction-type="JTA">
The EntityManagerFactory bean:
It looks like something is missing when using LocalContainerEntityManagerFactory. I have tried to use LocalContainerEntityManagerFactory.packagesToScan and it seems to me that the code (DefaultPersistenceUnitManager.buildDefaultPersistenceUnitInfo) that builds PersistenceUnitInfo is not setting the transaction-type as it should do.
By now I´ll use this approach until some news about it.
Thanks for answering me.