10 Replies Latest reply on Sep 1, 2011 7:09 PM by chandraprabha

    fuse servicemix 4.4 -JTA

    chandraprabha

      Hi ,

       

      Curretly I am using jpa,spring and hibernate in my webservice project now I want to add distributed transaction support to it . Kindly let me know how I can do this.

       

      Deploying the application as OSGi bundle in servicemix.

        • 1. Re: fuse servicemix 4.4 -JTA
          janstey

          There is an example here https://github.com/cmoulliard/sparks/tree/master/fuse-webinars/camel-persistence-part2/dao-jta using OpenJPA. Should be similar with Hibernate if you are using JPA config rather than Hibernate specific config.

           

          Cheers,

          Jon

          • 2. Re: fuse servicemix 4.4 -JTA
            chandraprabha

            Thanks a lot for the example.

             

            Kindly let me know if there is some example for handaling multiple entityManagerFactory.

             

            1.Oracle

            2.Postgres

             

            And i want to add the transaction support at business layer .

             

            business layer function

             

            public void insertUpdateData(){

             

            oracleDao.getVal();

            postGreDao.getVal();

             

            }

            • 3. Re: fuse servicemix 4.4 -JTA
              janstey

              I think you will need to create a persistence unit for each DB like explained here:

               

              http://stackoverflow.com/questions/1902997/multiple-database-with-springhibernatejpa

              • 4. Re: fuse servicemix 4.4 -JTA
                chandraprabha

                what should be the value of hibernate.transaction.manager_lookup_class in case of servicemix

                • 5. Re: fuse servicemix 4.4 -JTA
                  chandraprabha

                  I also read on fuse site :

                  The X/Open XA standard describes a standardized interface for integrating resources with a transaction manager. If you want to manage a transaction that includes more than one resource, it is essential that the participating resources support the XA standard. Resources that support the XA standard expose a special object, the XA switch, which enables transaction managers (or TP monitors) to take control of their transactions. The XA standard supports both the 1-phase commit protocol and the 2-phase commit protocol

                   

                   

                  Please let meknow if there is any example of this.

                  • 6. Re: fuse servicemix 4.4 -JTA
                    janstey

                    I haven't looked at the latest version of Hibernate so I'm not sure if they have OSGi support for this type of element yet. In the past I just created a custom one like so:

                     

                    import java.util.Properties;
                    
                    import javax.transaction.Transaction;
                    import javax.transaction.TransactionManager;
                    
                    import org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader;
                    import org.hibernate.transaction.TransactionManagerLookup;
                    import org.osgi.framework.BundleContext;
                    import org.osgi.framework.InvalidSyntaxException;
                    import org.osgi.util.tracker.ServiceTracker;
                    
                    public class OsgiTransactionManagerLookup implements TransactionManagerLookup {
                        private static TransactionManager transactionManager;
                        
                        public TransactionManager getTransactionManager(Properties props) {
                            try {
                                if (transactionManager == null) {
                                    DefaultClassLoader classLoader = (DefaultClassLoader) this.getClass().getClassLoader();
                                    BundleContext context = classLoader.getBundle().getBundleContext();
                                    ServiceTracker serviceTracker = new ServiceTracker(context, context.createFilter("(objectClass=javax.transaction.TransactionManager)"), null);            
                                    serviceTracker.open();
                                    transactionManager = (TransactionManager) serviceTracker.getService();
                                }
                            } catch (InvalidSyntaxException e) {
                                throw new IllegalStateException("No javax.transaction.TransactionManager found as OSGi service.", e);
                            }
                            
                            return transactionManager;
                        }
                    
                        public String getUserTransactionName() {
                             return "java:comp/UserTransaction";  
                         }
                    
                        public Object getTransactionIdentifier(Transaction transaction) {
                            return transaction;
                        }
                    }
                    

                     

                    • 7. Re: fuse servicemix 4.4 -JTA
                      janstey

                      This just means if you want to use XA (like enlisting several resources like brokers, DBs, etc in a single TX) you have to make sure the resources themselves are XA aware. Oracle definitely is and probably Postgres too.

                      • 8. Re: fuse servicemix 4.4 -JTA
                        chandraprabha

                        Thanks for response.

                         

                        I am getting Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager

                         

                        ..

                        1. I have daos where i am using persistence unit to enject the entityManager

                        2. in Service layer i have function which call oracleDao and PostgresDao

                         

                        My persistence_oracle.xml

                         

                         

                        • 9. Re: fuse servicemix 4.4 -JTA
                          chandraprabha

                          Can any one please help

                          • 10. Re: fuse servicemix 4.4 -JTA
                            chandraprabha

                            The OsgiTransactionManagerLookup is throwing :

                             

                            Caused by: java.lang.ClassCastException: org.apache.felix.framework.ModuleImpl$ModuleClassLoaderJava5 cannot be cast to org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader

                                    at com.luthresearch.surveysavvy.interim.business.service.util.OsgiTransactionManagerLookup.getTransactionManager(OsgiTransactionManagerLookup.java:19)

                                    at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:367)

                                    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)

                                    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)

                                    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)

                                    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:132)

                                    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)

                                    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)

                                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)

                                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)

                                    ... 29 more