10 Replies Latest reply on May 25, 2008 6:56 PM by alrubinger

    Bean Managed Transaction: No JTA transaction found

    rituraj_tiwari

      I am resorting to BMT due to the need to launch persistence operations in worker threads.

      I am using the BMT format as described here http://www.hibernate.org/hib_docs/entitymanager/reference/en/html/transactions.html

      So, I have

      @PersistenceUnit EntityManagerFactory m_emf;
      @Resource UserTransaction m_ut;
      


      And my worker threads do the equivalent of
      public Result call() {
       EntityManager em = factory.createEntityManager();
       try {
      
       // do some work
       ...
      
       m_ut.commit();
       }
       catch (RuntimeException e) {
       if (m_ut != null) m_ut.rollback();
       throw e; // or display error message
       }
       finally {
       em.close();
       }
      


      I observe that any entities I create aren't being written to the database. Looking in JBoss logs from the EntityManager creation point, I see:

      2008-05-24 12:39:10,911 DEBUG [org.hibernate.ejb.AbstractEntityManagerImpl] Looking for a JTA transaction to join
      2008-05-24 12:39:10,911 DEBUG [org.hibernate.ejb.AbstractEntityManagerImpl] No JTA transaction found
      


      Also, When I look at what happens when I am creating and persisting entities, I see:
      2008-05-24 12:40:05,036 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] delaying identity-insert due to no transaction in progress^M
      


      Am I making some obvious mistake? Is launching persistent operations from separate threads not supported?

      Thanks.
      -Raj

        • 1. Re: Bean Managed Transaction: No JTA transaction found
          rituraj_tiwari

          An update on this issue:
          I moved the entity manager creation code inside my worker thread so as to skirt any issues with transactions being threadlocal etc. I stilll cannot find a way to get a JTA transaction:

          ...
          
           InitialContext ctx = new InitialContext();
           EntityManagerFactory emf = (EntityManagerFactory)ctx.lookup( "java:/MyProjectEntityManagerFactory" );
          
           UserTransaction ut = (UserTransaction)ctx.lookup( "java:comp/UserTransaction" );
          
          ...
          

          I am able to get a hold of valid objects. Still, I get the same messages about no JTA transactions being found. Of course, any persistent operations I do get queued up and don't get written to the database.

          Is this a known bug in JBoss' EJB3 or Hibernate layers?

          I have been hammering on this problem for three days non-stop. Any help from someone out there will be greatly appreciated.

          Thanks.
          -Raj

          • 2. Re: Bean Managed Transaction: No JTA transaction found
            alrubinger

            I don't see in your code samples where you're calling UserTransaction.begin()...

            S,
            ALR

            • 3. Re: Bean Managed Transaction: No JTA transaction found
              rituraj_tiwari

              Al,
              Thanks for your response. I am doing ut.begin() right after I create the user transaction. This is a simplified version of my worker thread's method:

               InitialContext ctx = new InitialContext();
               EntityManagerFactory emf = (EntityManagerFactory)ctx.lookup( "java:/MyProjectEntityManagerFactory" );
               UserTransaction ut = (UserTransaction)ctx.lookup( "java:comp/UserTransaction" );
              
               EntityManager em = emf.createEntityManager();
              
               ut.begin();
               doPersistentStuff(em);
               ut.commit();
              


              I am making sure that there is a single thread that creates EntityManagerFactory, EntityManager and performs the persistent operations.


              • 4. Re: Bean Managed Transaction: No JTA transaction found
                alrubinger

                Have you defined your EntityManager as "resource-local" in persistence.xml?

                S,
                ALR

                • 5. Re: Bean Managed Transaction: No JTA transaction found
                  rituraj_tiwari

                  No. I did not realize that would be needed since it seems to work just fine when invoked from session beans. I want to use the same, working infrastructure, but from a worker thread.

                  That being said, this is my persistence.xml:

                  <persistence-unit name="MyProject">
                   <provider>org.hibernate.ejb.HibernatePersistence</provider>
                   <jta-data-source>java:/MyProjectDatasource</jta-data-source>
                   <properties>
                   <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
                   <property name="hibernate.hbm2ddl.auto" value="update"/>
                   <property name="hibernate.show_sql" value="true"/>
                   <property name="hibernate.format_sql" value="true"/>
                   <property name="jboss.entity.manager.factory.jndi.name" value="java:/MyProjectEntityManagerFactory"/>
                   </properties>
                   </persistence-unit>
                  


                  Thanks for your help in the wee hours of the morning!

                  -Raj

                  • 6. Re: Bean Managed Transaction: No JTA transaction found
                    alrubinger

                    Been awhile since I did BMT; you might also try creating your EntityManager *after* starting the JTA Transaction, seeing if that will enlist it with the Tx in the current Thread.

                    S,
                    ALR

                    • 7. Re: Bean Managed Transaction: No JTA transaction found
                      rituraj_tiwari

                      Andrew,
                      That is really what I am trying to figure out here: How do I start a JTA transaction? I know that one is magically available to me if I am running inside an EJB (or a Seam Component). It is not clear how I go about starting one if I am in this worker thread that was launched by my own code.

                      -Raj

                      • 8. Re: Bean Managed Transaction: No JTA transaction found
                        alrubinger

                        http://java.sun.com/j2ee/1.4/docs/api/javax/transaction/UserTransaction.html#begin()

                        ...should be handling this for you. Does creating your EM after calling "begin" properly enlist the EM with the Tx?

                        S,
                        ALR

                        • 9. Re: Bean Managed Transaction: No JTA transaction found
                          rituraj_tiwari

                          Andrew,
                          This is the best thing I have heard all week!!

                          Of course, I would look at my logs and everytime I created the entity manager from my entitymanager factory it would say that there was no active JTA transaction to join.

                          Now I have flipped the order of processing:
                          - I first create the UserTransaction and begin() is
                          - Only then do I ask the entity manager factory for an entity manager.

                          This makes my stuff work.

                          This does beg the question: Is it a requirement that I create an entity manager only after a user transaction has been started? Shouldn't my entity manager be able to latch onto a user transaction that gets started later?

                          Let me know if this sounds like bug material to you.

                          You just saved me a lot of anguish here. If you are in the SF bay area, drop me a line. I will happily buy you a beer!

                          -Raj

                          • 10. Re: Bean Managed Transaction: No JTA transaction found
                            alrubinger

                             

                            "rituraj_tiwari" wrote:
                            Let me know if this sounds like bug material to you.


                            No, works as I would infer from:

                            "EJB3 Persistence Spec 5.5.1" wrote:
                            A JTA entity manager participates in the current JTA transaction, which is begun and committed external to the entity manager and propagated to the underlying resource manager.


                            My interpretation would stipulate that the EM would have to be created in the presence of a JTA Transaction, hence my guess earlier.

                            JBoss EJB3 delegates these operations out to the JPA Persistence Provider, in our case, Hibernate EM. So if you've got more detailed questions I'd ask over on the Hibernate forums.

                            "rituraj_tiwari" wrote:
                            You just saved me a lot of anguish here. If you are in the SF bay area, drop me a line. I will happily buy you a beer!


                            Just was there last month, actually. :) Beautiful area.

                            S,
                            ALR