11 Replies Latest reply on Feb 6, 2014 6:06 AM by mcsreddy86

    Working with distributed transactions in SIF?

    Seasoned Veteran

      Hi,

      I am using MDM9.5 and SIF API. I am using EJB protocol and managing the transactions using UserTransaction and is working fine.

       

      Now i have below code.

       

       

                EjbSiperianClient ejbClient = (EjbSiperianClient)client;

                  InitialContext initialContext = ejbClient.getInitialContext();

               

                  tx = (UserTransaction)initialContext.lookup("UserTransaction");

                

                  tx.begin();

                

                 //Update/Insert data using SIF API

                 //Save some other data into some table using hibernate

                  tx.commit();

       

       

      Now before committing, after saving the data into MDM hub, i want to save some information into separate table using Hibernate. Now Hibernate and MDM runs in separate transactions. Can i use any distributed transactions for this functionality? Does MDM supports anything like this?

       

      Thanks!

        • 1. Re: Working with distributed transactions in SIF?
          Subbu K Guru

          MDM SIF uses container managed EJB transactions. So it supports distributed transactions, as they are controlled by the container themselves. So you can integrate Hibernate as per your requirement.

          • 2. Re: Working with distributed transactions in SIF?
            Denis Toporov Seasoned Veteran

            Subbu provided correct answer. However I have a question regarding your code sample - why do you choose to use explicit transaction management in your code ?

            • 3. Re: Working with distributed transactions in SIF?
              Guru

              Denis - As per my understanding yes you are right that transactions are handled by container... but only for individual SIF call, say if anything wrong happens in a PUT call things will be rolled back.

               

              However here the usecase of Sekhar might be group of SIF calls say PUT1, PUT2, PUT3 etc. and if any one fails then he wants to rollback all other SIF PUT calls , that is why we need to explicitly initialize a Transaction and place all the SIF calls enclosed inside tx.begin() and tx.commit or tx.rollback().

               

              Hope it answers the query.

              • 4. Re: Working with distributed transactions in SIF?
                Denis Toporov Seasoned Veteran

                According to Java Enterprise Edition specs there are two ways to manage transactions Container Managed Transaction CMT and Bean Managed Transaction BMT.

                 

                Container-Managed Transactions - The Java EE 5 Tutorial

                 

                "In an enterprise bean with container-managed transaction demarcation, the EJB container sets the boundaries of the transactions. You can use container-managed transactions with any type of enterprise bean: session, or message-driven. Container-managed transactions simplify development because the enterprise bean code does not explicitly mark the transaction’s boundaries. The code does not include statements that begin and end the transaction.

                By default if no transaction demarcation is specified enterprise beans use container-managed transaction demarcation.

                Typically, the container begins a transaction immediately before an enterprise bean method starts. It commits the transaction just before the method exits. Each method can be associated with a single transaction. Nested or multiple transactions are not allowed within a method.

                Container-managed transactions do not require all methods to be associated with transactions. When developing a bean, you can specify which of the bean’s methods are associated with transactions by setting the transaction attributes.

                Enterprise beans that use container-managed transaction demarcation must not use any transaction management methods that interfere with the container’s transaction demarcation boundaries. Examples of such methods are the commit, setAutoCommit, and rollback methods ofjava.sql.Connection or the commit and rollback methods of javax.jms.Session. If you require control over the transaction demarcation, you must use application-managed transaction demarcation.

                Enterprise beans that use container-managed transaction demarcation also must not use the javax.transaction.UserTransaction interface."

                 

                 

                It's generally recommended to use CMT unless you have specific scenario that cannot be handled by CMT.

                 

                So in the following code sample all put() calls will be handled by container as one transaction and will commit or rollback

                 

                @TransactionAttribute(REQUIRES_NEW)

                  public void firstMethod() {

                put_1()

                put_2()

                put_3()

                .....

                put_n()

                }

                • 5. Re: Working with distributed transactions in SIF?
                  Seasoned Veteran

                  Hi Sunil/Denis,

                  Sunil, as you explained, that is the my requirement. I need to make group of SIF PUT calls and one call to save some data into table which is not part of MDM hub. MDM SIF PUT calls and Hibernate calls should be in single transaction. So that all will be rolled back if some thing goes wrong. does MDM support such transactions? Thanks!

                   

                  As of now making multple SIF PUT calls is successful using below the code.

                   

                  EjbSiperianClient ejbClient = (EjbSiperianClient)client;

                              InitialContext initialContext = ejbClient.getInitialContext();

                          

                              tx = (UserTransaction)initialContext.lookup("UserTransaction");

                           

                              tx.begin();

                           

                             //Make multiple PUT calls

                             //Save some other data into some table using hibernate

                              tx.commit();


                  Now after making multiple calls, data is saved into BO tables. After that i have to save some data into some table which is not part of MDM hub using hibernate. Saving data into MDM hub BO tables and Saving data into External table of different schema should be in a single transaction.. Can i do that? Thanks!

                  • 6. Re: Working with distributed transactions in SIF?
                    Denis Toporov Seasoned Veteran

                    Yes you can if your persistence layer runs in an application server (for example, behind EJB session beans), every datasource connection obtained by Hibernate will automatically be part of the global JTA transaction.

                     

                     

                    @TransactionAttribute(REQUIRES_NEW)

                      public void firstMethod() {

                    put_1()

                    put_2()

                    put_3()

                    .....

                    put_n()

                     

                     

                    // CMT idiom

                    Session sess = factory.getCurrentSession();

                     

                    // do some work

                    ...

                    }

                    • 7. Re: Working with distributed transactions in SIF?
                      Seasoned Veteran

                      Hi Denis, Is @TransactionAttribute(REQUIRES_NEW) part of EJB or Spring AOP ?

                       

                      Do i need to use JTA transaction Manager?

                       

                      Thanks!

                      • 8. Re: Working with distributed transactions in SIF?
                        Denis Toporov Seasoned Veteran

                        Annotation @TransactionAttribute(REQUIRES_NEW) is a part of javaee ejb. Spring should have similar e.g. @Transactional(propagation = Propagation.REQUIRES_NEW)

                        Yes, JTA manager required to run distributed transactions

                         

                        • 9. Re: Working with distributed transactions in SIF?
                          Seasoned Veteran

                          Hi Denis,

                          Thanks for your reply. In this case i can completely avoid using UserTransaction right?



                          Thanks!

                          • 10. Re: Working with distributed transactions in SIF?
                            Seasoned Veteran

                            Hi Denis,

                             

                            As per your answer, do i need to use JTATransactionManager?

                             

                            Thanks!

                            • 11. Re: Working with distributed transactions in SIF?
                              Seasoned Veteran

                              Hi Subbu,

                              Regarding distributed transactions, do i need to use JTATransactionManager? If so, we need register session factories with JTATransactionManager and each session factory should have connection details. But in my case how can i register SiperianClient with JTATransactionManager? I can register Hibernate SessionFactory without any issues?

                               

                              Thanks!