2 Replies Latest reply on Nov 17, 2006 9:35 PM by Jacek Kowalski

    jta transactions - simple question

    Jacek Kowalski Newbie


      Jboss: 4.0.5GA
      java: 1.4

      I think I'm missing something. Below you can find a simple piece of code. It is similar to every example which shows how to handle with jta transactions. It is from my stateful session bean. Ok lets look at that:

      UserTransaction uT = sessionContext.getUserTransaction();
       try {
       }catch (Exception ex) {
       try {
       System.out.println("Caught exception: " + ex.getMessage());
       System.out.println("rollback OK");
       } catch (Exception rex){
       System.out.println("Caught exception: " + rex.getMessage());
       throw new EJBException("Rollback failed: " + rex.getMessage());
       throw new EJBException("Transaction failed: " + ex.getMessage());

      I expected to catch exception thrown by commit() method due to transaction rolled back. Then rollback transaction, and... the end. But something doesn't work like i expected. There is an exception thrown by method rollback(). Why?

      Below interesting output:

      22:29:44,625 INFO [STDOUT] Caught exception: Already marked for rollback TransactionImpl:XidImpl[FormatId=257, GlobalId=LENOVO-98B03F24/89, BranchQual=, localId=89] OK
      22:29:44,625 INFO [STDOUT] *********************** ???
      22:29:44,671 INFO [STDOUT] Caught exception: No transaction.???
      22:29:44,671 ERROR [LogInterceptor] EJBException in method: public abstract void home.main.Main.rollbackMethod() throws java.rmi.RemoteException:
      javax.ejb.EJBException: Rollback failed: No transaction.
      at home.main.MainBean.rollbackMethod(MainBean.java:128)

      Can you explain me why i cannot roll back my transaction?


        • 1. Re: jta transactions - simple question
          Elias Ross Master

          I don't believe you can do a commit() following a setRollbackOnly(), use rollback() or commit().

          • 2. Re: jta transactions - simple question
            Jacek Kowalski Newbie


            Thank you for your answer. I put uT.setRollbackOnly() only to check what would happen. Now I've changed this line to:

            throw new Exception("dark force exception")

            and rollback method in catch (Exception ex) works quite fine.
            So i belive that failed commit() method is the reason of missed transaction.

            I belive also that when i can mark transaction for rolback i must check if the transaction is marked for rollback before call commit() method.

            I think the best way will be replace commit() with:

            if (uT.getStatus() == Status.STATUS_MARKED_ROLLBACK)
             throw new Exception("dark force exception");

            I think throwing an exception is a good thing. Exception will be caught, and efter rollback will be thrown to the client.
            Thanks to that client will know that something wrong has happened.

            Am I right?