10 Replies Latest reply on Feb 14, 2013 4:17 PM by pbalachandran

    Setting up Infinispan as Hibernate L2 Cache & Transaction Management

    pbalachandran

      I haven't been able to successfully setup Infinispan as a Hibernate L2 Cache and enable transaction management in JBoss AS7.1.0.Final.

       

       

      a. I got the Hibernate L2 part working with these settings but I am not sure if these are accurate.

      b. However I am unable to introduce JTA transactions to the equation.

       

      Please refer to my current hibernate.cfg.xml, DAOs and HibernateUtil (session mgt) and HibernateInfiManager (transaction mgt) classes attached.

       

      I tried to vary the settings inside the hibernate.cfg.xml file based on the posts (hibernate.cache.region.factory_class, hibernate.transaction.manager_lookup_class - deprecated, along with CMTTransactionFactory versus JtaTransactionFactory), but I get exceptions ranging from NPE, to Unable to locate Current JTA Transaction etc.

       

      Any help is appreciated.

       

      Thank you.

        • 2. Re: Setting up Infinispan as Hibernate L2 Cache & Transaction Management
          pbalachandran

          I tried my setup with jboss-as-7.1.1.final as well as the latest nightly build (jboss-as-7.2.0.Alpha1-SNAPSHOT) and both give me the same error - Unable to locate current JTA Transaction.

           

          org.hibernate.HibernateException: Unable to locate current JTA transaction

          org.hibernate.context.internal.JTASessionContext.currentSession(JTASessionContext.java:88)

          org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:883)

          com.corelogic.dao.HibernateUtil.getSession(HibernateUtil.java:55)

          com.corelogic.dao.HibernateUtil.beginTransaction(HibernateUtil.java:34)

          com.corelogic.infinispan.HibernateInfiManager.findAll(HibernateInfiManager.java:30)

           

           

          I am using the following properties inside my hibernate.cfg.xml file and I get the same error, for both combinations (a && c) or (b && c) below.

           

          a. hibernate.transaction.factory_class = org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory

           

          b. hibernate.transaction.factory_class = org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory

           

          c. hibernate.transaction.jta.platform = org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform

           

          Is there something that needs to be added inside the code to lookup the transaction manager, prior to a beginTransaction?  Please refer to HibernateInfiManager.java (in original post) where I begin and end transactions.

           

          Much appreciated.

          • 3. Re: Setting up Infinispan as Hibernate L2 Cache & Transaction Management
            smarlow
            1 of 1 people found this helpful
            • 4. Re: Setting up Infinispan as Hibernate L2 Cache & Transaction Management
              pbalachandran

              I modified my code to look similar to the classes you had attached and also within HibernateUtil.java:

               

              a. Set the hibernate.transaction.jta.platform to org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform

              b. Enabled a datasource to hit the MySQL DB, which is being referenced via configuration.setProperty(Environment.DATASOURCE, "java:jboss/datasources/mysqldbDS")

               

              However I still get the same error:

               

              org.hibernate.HibernateException: Unable to locate current JTA transaction

              org.hibernate.context.internal.JTASessionContext.currentSession(JTASessionContext.java:88)

              org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:883)

              com.corelogic.dao.HibernateUtil.getSession(HibernateUtil.java:55)

              com.corelogic.dao.HibernateUtil.beginTransaction(HibernateUtil.java:34)

              com.corelogic.infinispan.HibernateInfiManager.findAll(HibernateInfiManager.java:30)

               

              Few questions:

               

              a. When JTA is enabled, is it expected to automatically create a transaction and make it avaialable for the session to use?  If that is so, I would not need to begin/commit transactions inside the code?

              b. If so which transaction manager is being used to create the said transaction - I think it is looks up JNDI name "java:jboss/TransactionManager" (I believe this code is in JBossAppServerJtaPlatform.java)

              c. What is JNDI name java:comp/UserTransaction - is this the transaction that seems to "not exist"?

               

              Are these existing bugs in Infinispan/Hibernate that haven't been addressed?

               

              If you have some time, please see updated files attached.

               

              Thank you.

              • 5. Re: Setting up Infinispan as Hibernate L2 Cache & Transaction Management
                pbalachandran

                Does anyone know how to solve for this issue?  If there are any temporary workarounds please post.

                 

                Thank you.

                • 6. Re: Setting up Infinispan as Hibernate L2 Cache & Transaction Management
                  smarlow

                  Few questions:

                   

                  a. When JTA is enabled, is it expected to automatically create a transaction and make it avaialable for the session to use?  If that is so, I would not need to begin/commit transactions inside the code?

                  b. If so which transaction manager is being used to create the said transaction - I think it is looks up JNDI name "java:jboss/TransactionManager" (I believe this code is in JBossAppServerJtaPlatform.java)

                  c. What is JNDI name java:comp/UserTransaction - is this the transaction that seems to "not exist"?

                  You should arrange for the JTA transaction to start in your application (e.g. use an EJB session bean to start/end the JTA transactions.)

                   

                  Regarding your question about java:comp/UserTransaction, your not finding the UserTransaction because its not setup on the current component that your invoking from. 

                  1 of 1 people found this helpful
                  • 7. Re: Setting up Infinispan as Hibernate L2 Cache & Transaction Management
                    pbalachandran

                    Thank you Scott for the reply. 

                     

                    I went over the example you had attached from a couple of days ago, again, and noticed that the code was doing the following:

                     

                    a. Session session = sessionFactory.openSession()

                    b. DB Operation (save, find etc)

                    c. session.flush()

                    d. session.close()

                     

                    Essentially the code was opening and closing sessions for each access to the DB via the cache, as opposed to doing a sessionFactory.getCurrentSession().  When I made this modification (sessionFactory.getCurrentSession() to instead use sessionFactory.openSession()), JTA started working.  Not sure if this is the correct paradigm for JTA based transaction management.

                     

                    Please let me know.  Once again I appreciate your feedback on this topic.

                     

                    Thank you.

                    • 8. Re: Setting up Infinispan as Hibernate L2 Cache & Transaction Management
                      smarlow

                      Sure, you can just open sessions, use them and close them.  Check the Hibernate documentation here and if you have more Hibernate specific questions also refer to other resources here (see Hibernate user forum under community link).

                      • 9. Re: Setting up Infinispan as Hibernate L2 Cache & Transaction Management
                        pbalachandran

                        Scott:

                         

                        Picking up from the above post, how does one validate that the Hibernate L2 cache is even in play.  I am running  a small web application that is reading and writing one entity to a MySQL DB via a datasource in JBoss AS7.  Based on some articles, it appears that you can view the cache statistics using JConsle (please see attached snapshot), which I tried.  I see the infinispan module, but not sure what to make of the details.  Also I don't see the named entity (or instances of that entity) that I am writing to and reading from the DB.

                         

                        Appreciate any feedback.

                         

                        Thank you.

                        • 10. Re: Setting up Infinispan as Hibernate L2 Cache & Transaction Management
                          pbalachandran

                          Anyone?

                           

                          I suspect that my entity is not even being cached (meaning Infinispan's is not auto enabling the L2 cache even though I have set the hibernate.cache.use_second_level_cache=true.

                           

                          Does Infinispan have any recent documentation on this - much of the documentation seems outdated and wrong.

                           

                          Thank you.