4 Replies Latest reply on May 26, 2008 2:05 AM by jaikiran pai

    Doing persistence operations in worker threads

    Raj Tiwari Newbie

      Folks,
      I am running into a problem where I have come to a dead end after following several leads.

      I am launching an operation asynchronously in a separate thread which gathers some info and then starts persisting some entities. Problem is, I cannot seem to get anything written to the database.

      My current approach is to get the EntityManagerFactory and UserTransaction in my worker thread by doing a JNDI lookup:

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


      As soon as the last line above is executed, I see the following message in my JBoss log file:
      2008-05-25 01:14:30,093 DEBUG [org.hibernate.impl.SessionImpl] opened session at
       timestamp: 4963136594300928
      2008-05-25 01:14:30,093 DEBUG [org.hibernate.ejb.AbstractEntityManagerImpl] Looking for a JTA transaction to join
      2008-05-25 01:14:30,093 DEBUG [org.hibernate.ejb.AbstractEntityManagerImpl] No JTA transaction found
      


      All persistent operations seem to work, but nothing gets written to the DB. I see the following log messages in server.log:
      2008-05-25 01:21:20,718 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] delaying identity-insert due to no transaction in progress
      


      Is there anything I can do to start a JTA transaction?

      My environment is JBoss 4.2.2 and Seam 2.x

      Thanks.
      -Raj

        • 2. Re: Doing persistence operations in worker threads
          Raj Tiwari Newbie

           

          "jaikiran" wrote:
          Please do not post the same question in multiple forums http://www.jboss.com/index.html?module=bb&op=viewtopic&t=136149


          Please do not post a response if you cannot be helpful.

          Given my problem, its unclear which camp it belongs in: Seam, Hibernate, JTA, Jboss EJB. Given that there is a forum called EJB3.0 and another one called JBoss/EJB, I think my cross-posting to EJB3.0 is completely appropriate.

          I see that you are a frequent poster here and I commend you on your work for the community. Let me know if you have never advised someone to move their post to another forum. What would have been better is if you read my question and told me what the right forum was.

          -Raj

          • 3. Re: Doing persistence operations in worker threads
            Raj Tiwari Newbie

            Folks,
            Here is a resolution to my problem:

            What fixed this for me was doing
            UserTransaction.begin() before creating the entity manager from the entity manager factory.

            If I create my entity manager and then start the user transaction, the entity manager is never able to associate with the started transaction. All persistent operations go to the abyss.

            Not sure if this is a bug or known and accepted behaviour.

            -Raj

            • 4. Re: Doing persistence operations in worker threads
              jaikiran pai Master

               

              "rituraj_tiwari" wrote:

              Please do not post a response if you cannot be helpful.

              Given my problem, its unclear which camp it belongs in: Seam, Hibernate, JTA, Jboss EJB. Given that there is a forum called EJB3.0 and another one called JBoss/EJB, I think my cross-posting to EJB3.0 is completely appropriate.


              Raj,

              Posting in multiple forums (unless someone actually asks you to do it) wastes people's time who try to solve your problems. It also creates duplicate conversations.

              "rituraj_tiwari" wrote:


              I see that you are a frequent poster here and I commend you on your work for the community. Let me know if you have never advised someone to move their post to another forum. What would have been better is if you read my question and told me what the right forum was.



              I have advised people to post in a different forum if the original question did not evoke enough responses or if the forum was not right. If you look at my earlier reply, i have pasted a link to your other discussion so that if anyone ever runs into this thread, he will be able to find a discussion in the other thread. The intention was to keep the discussion at one place.