4 Replies Latest reply on Dec 29, 2005 12:41 AM by Thomas Orr

    Cancel a Rollback

    Thomas Orr Newbie

      Maybe my approach needs to be altered, but how do I cancel a rollback in EJB3?

      My situation is that I have one session bean method that is wrapped in a transaction. This method makes two calls to the database. The first call is to see is a records exists. The second inserts a new record if the first call comes up empty. The problem is that when I check to see if the entity exists, I get an EntityNotFoundException. That's fine. That's what I expect. The problem is that this exception rolls back the entire transaction so that I cannot make the subsequent insert. This happens no matter how I try to catch the exceptions.

      These two operations must execute atomically. So, is there anyway to ignore an exception like this or cancel the rollback?

      Thanks
      T

        • 1. Re: Cancel a Rollback
          Bill Burke Master

          So, your ENFE exception is propagated to the nester of the EJB? If a runtime exception gets thrown pass an EJB boundary then the transaction is marked for rollback.

          You can't get around this. Refactor your code to use find() and check for NULL or catch the ENFE exception within the nested EJB call and wrap it with a non-rollbacking exception (checked or runtime annotatied with @ApplicationException(rollback=false)), or, just change the contract of your nested EJB invocation.

          • 2. Re: Cancel a Rollback
            Thomas Orr Newbie

            That's unfortunate for me. How can you use find with a native SQL query? I think that using @ApplicationException(rollback=false) will not be so great for me because the Exception I am raising should rollback transactions under certain circumstances. I know I could create two different exceptions, but that doesn't feel right.

            If I demarcate the first operation as requiring a new transaction, thereby nesting the transaction, and then it throws an exception that I catch will the parent trans rollback?

            • 3. Re: Cancel a Rollback
              Bill Burke Master

              that won't work either as you won't have the same persistence context and the returned entity won't be managed anymore.

              I fhink you need to rearchitect your API as suggested above.

              BTW, your scenario is exactly the reason why the spec has

              find() which returns null
              and getReference() which throws an ENFE.

              • 4. Re: Cancel a Rollback
                Thomas Orr Newbie

                Again, unfortunately find is not an option because I use native sql. I have moved an existance check into my stored proc and it works fine now. It is not ideal, but it works.

                Thanks