Results 1 to 5 of 5

Thread: Spring + Hibernate +JTA - HibernateTransactionManager or JTATransactionManager

  1. #1
    Join Date
    Mar 2011
    Posts
    3

    Default Spring + Hibernate +JTA - HibernateTransactionManager or JTATransactionManager

    Hi,

    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?

    Thanks.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Mar 2011
    Posts
    3

    Default

    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();
    //logic
    tx.commit();

    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.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Mar 2013
    Posts
    1

    Default

    Hi Marten,

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •