May 9th, 2011, 07:22 AM
Spring + Hibernate +JTA - HibernateTransactionManager or JTATransactionManager
I want to Integrate Spring and Hibernate and use JTA for managing the transactions. I have a query regarding the configuration of the "transactionManager" bean in Spring:
1) Can we use HibernateTransactionManager and set the the properties "hibernate.transaction.factory_class" and "hibernate.transaction.manager_lookup_class" in hibernate.properties file?
If not, why?
2) Or, should we use JTATransactionManager?
In option (2), do we still need to set the properties("hibernate.transaction.factory_class", "hibernate.transaction.manager_lookup_class") in hibernate.properties?
May 9th, 2011, 07:59 AM
Option 2.... You have to use the tx manager for the technology you use, you want to use JTA driven transactions then use JTA transaction manager and configure hibernate accordingly... HibernateTransactionManager is only for managing local hibernate transactions. So you have to use 1 and set the hibernate.transaction.factory_class and hibernate.transaction.manager_lookup_class properties of hibernate.
May 9th, 2011, 08:24 AM
Thanks Marten. Just still not convinced why we should not use HibernateTransactionManager ... Since Hibernate's Transaction interface essentially wraps the underlying transaction mechanism, there should not be any change in the application code whether we use JDBCTransaction or JTATransaction. Based on the properties hibernate.transaction.manager_lookup_class and hibernate.transaction.factory_class, Hibernate determines which mechanism to use... (i.e.) the code would still remain as:
Transaction tx = session.beginTransaction();
So, my thought is that even if we use HibernateTransactionManager and enable the settings (hibernate.transaction.manager_lookup_class and hibernate.transaction.factory_class) in hibernate.properties file, it should still be fine...
Please correct me if I am missing anything here.
May 9th, 2011, 08:52 AM
Well yes and no... If you use multiple resources (jms and hibernate together for instance) your setup is going to fail as you should only have 1 tx manager (and not 1 for hibernate, 1 for jms, 1 for another xa tx resource)... As you really should be using the jta resources to manage your transactions (and not hibernate) as you have left the tx part to spring (and not hibernate) so spring should detect the ongoing jta transaction (and register the ongoing transaction with the current thread as well as the used resources) if you would use the HibernateTransactionManager, this would fail and spring would try to start a new transaction while there would be already 1 going on.
Mar 5th, 2013, 10:13 AM
Does it mean if using HibernateTransactionManager with different hibernate datasource (1 sessionfactory per jar). Each jar is packaged in different ear but deployed in the same instance. No other jms / xa resource. This configuration can enjoy the transaction managemnt by container ?
Actually, I have made a test ( multiple ear on same server instance) and try throws RunTimeException in one of the ejb. The action on another ejb can be rollbacked as well. That made me think that saravana_pc approach seems work in some situation.
Can you advise? Thanks very much.
Tags for this Thread