6 Replies Latest reply on May 26, 2009 3:27 PM by Israel Fonseca

    Conversations, Transactions and Rollback

    Israel Fonseca Apprentice

      i have the following situation:


      @In
      EntityManager em;


      public void persistUser(User user, Stuff stuff){


      try{
      em.persist(user); //ok
      em.persist(stuff); // runtime in here error
      em.flush
      } catch(Exception e) {
      FacesMessage.instance().add(Error!);
      return; //now i have a ghost user that will be persisted next time (if it suceeds)
      }


      }


      It's executed in a long-running conversation (using seam managed transactions), and with manual-flushing enabled. The problem is: how can i clean my first user persist. I'm still in a long running conversation and i dont want to end it (because i have a list with objects from the database in the screen, and i want to keep them managed for now). But as far i know if in the next time i have a successful perist it will make TWO users persisted. Am I right?


      I wanted to clear the operations that i queued in the EntityManagers on that transaction. Is that possible?


      I cant use page exception handling cause it does a redirect and a page refresh. And the system needs to be fully implemented with Ajax requests.

        • 1. Re: Conversations, Transactions and Rollback
          Israel Fonseca Apprentice


          @In EntityManager em;
          
          public void persistUser(User user, Stuff stuff){
          
          try{ 
          em.persist(user); //ok 
          em.persist(stuff); // runtime in here error 
          em.flush 
          } catch(Exception e) { 
          FacesMessage.instance().add("Error!"); 
          return; //now i have a "ghost" user that will be persisted next time (if it suceeds) }
          
          } 



          Ops, i bugged the code. :)

          • 2. Re: Conversations, Transactions and Rollback
            Israel Fonseca Apprentice

            i tried something a bit different and i saw something very strange (nome more manual flushing):




            @In EntityManager em;
            
            @Transactional
            public void persistUser(User user, Stuff stuff){
            em.persist(user); //ok 
            em.persist(stuff); // runtime in here error 
            }
            




            I invoke this method from my backing-bean (another Seam component that injects this one), in that bean i have:



            try{
            bo.persistUser(user,stuff)
            }catch{
            FacesMessage.instance().add("error");
            } 
            List<User> u = em.createQuery("from User").getResultList();
            if(u.getSize() != 0)
            System.out.println("it should be like this");
            




            --


            I dont have users in my base, i try to persist but it fires an exception (should be rolled back). But when i do the search i find the user that i persisted(and shouldn't find).


            But.. it just find him for one request, if i search again afer this request, it is no more there (the way that it should work).


            Strange...

            • 3. Re: Conversations, Transactions and Rollback
              Francisco Jose Peredo Noguez Master

              You are more likely to get help if you post all  you stack trace.

              • 4. Re: Conversations, Transactions and Rollback
                Israel Fonseca Apprentice

                Ops, sorry let me retype.


                I dont have users in my base, i try to persist the stuff but its null so the entityManager fires a runtime exception. My backing bean catch this exception. At this time i suppose that the user that was persisted, was not, cause the transaction was rolledback (from the runtime exception on a transactional method), but it is still being retrivied from the database on that request. If i do a search after this, it is not there anymore.


                Does the transaction only get rolled-back after the invoke-method phase? I thought that the BMT would rollback as soon the method finishes.

                • 5. Re: Conversations, Transactions and Rollback
                  Arbi Sookazian Master

                  I typically use CMT with EJB (SFSB as  backing bean) but I believe that Seam managed tx's semantics are similar (i.e. like CMT).


                  stack trace = ERROR level (exceptions) logging in server.log


                  • 6. Re: Conversations, Transactions and Rollback
                    Israel Fonseca Apprentice

                    I'm using BMT now with EJB, so i'm happy for now. But, i wanted to know if is it possible to use two Persistence Contexts, Seam Managed one with the EJB managed one to use the s:convertEntity. I dont want seam to use global transactions too, cause i dont want that him touch my EJBs.