3 Replies Latest reply on Nov 17, 2007 1:06 PM by orhan yilmaz

    Transaction rolled back question

    Vinicius Carvalho Apprentice

      Hello there! I have an MDB that calls a SLSB to process a message.

      The SLSB perform some operations on the database. The SLSB captures all the exceptions that might happen never throwing them. But I'm getting in a sistuation where and SQLException happens, the Transaction is rolled back (even though my code captures it) and the message gets redelivered untill the threshold is reached.

      Here's some sample code:

      private void process(messageID){
      try{
      dao.updateMessage(messageID);
      }catch(Exception e){
      dao.recordError(messageID);
      }
      
      }
      
      //DAO:
      
      public void updateMessage(long messageID){
      try{...}
      catch(SQLEXception e){
      throw new RuntimeException(e);//softening the exception
      }
      


      Another issue, is that the record does not persist, even though it is annotated with REQUIRESNEW transaction attribute.

      I know that the container invalidate the transaction on a RuntimeException, but I tought it had to be thrown in order to the transaction be invalidated.

      Any suggestions.
      What's wrong here?
      REg

        • 1. Re: Transaction rolled back question
          orhan yilmaz Newbie

          if an excetion occurs container rollbacks automatically.
          if you want to hold the state of transaction use UserTransaction
          and this is not a good idea to catch exceptions

          try{...}
          catch(SQLEXception e){
          throw new RuntimeException(e);//softening the exception
          }

          can you publish dao.recordError(messageID); methods code.

          • 2. Re: Transaction rolled back question
            Vinicius Carvalho Apprentice

             

            "yilmaz_" wrote:
            if an excetion occurs container rollbacks automatically.
            if you want to hold the state of transaction use UserTransaction
            and this is not a good idea to catch exceptions
            try{...}
            catch(SQLEXception e){
            throw new RuntimeException(e);//softening the exception
            }

            can you publish dao.recordError(messageID); methods code.


            Well, I've just solved it by changing my app exception to a checked exception. The dao actually throwed an app exception which was an unchecked exception so I could not have to deal with it on every call.

            I was really hopping the conteiner would invalidate the exception only if my last call threw it :(

            Can't see why my application exceptions can not be descendants of RuntimeExceptions ...

            • 3. Re: Transaction rolled back question
              orhan yilmaz Newbie

              if you want to catch exception you can use throw

              dao.recordError(messageID);

              if recordError method throws an axception you may not catch it. You can use file logging instead of db logging. log4j or slf4j