3 Replies Latest reply on Mar 10, 2003 4:39 AM by allan

    Transaction handling for application level exception

    Sanjay Newbie

      Hello,
      I am using stateless session bean. If I get database level exception then all the transacitons are roledback. But if I get application level exception such as nullpointer,then in this case trasactions are not roledback. why is it so???????. Is there any way to handle this. Because I want transaction roled back for even a small exception. Awaiting your reply.

      Regds,
      Sanjay

        • 1. Re: Transaction handling for application level exception
          Adrian Brock Master

          try
          {
          ...
          }
          catch (Exception e)
          {
          context.setRollbackOnly();
          throw e;
          }

          Regards,
          Adrian

          • 2. Re: Transaction handling for application level exception
            Sanjay Newbie

            Hello Adrian,
            Thanks for the reply. But I did not get where to write the bolck of code you mentioned. I have a session bean which calls helper java class and my java class throws exception. So the code you mentioned should be written in session bean while calling that helper class or in the helper class which throws exception. Please elaborate. Awaiting your reply.

            Regds,
            Sanjay

            • 3. Re: Transaction handling for application level exception
              allan Newbie

              Hi Adrian,

              We are also having almost the same problem as Sanjay and we implemented your solution.

              We initialized the UserTransaction as

              UserTransaction userTxn = null;
              try {
              userTxn = (UserTransaction)ctx.lookup("UserTransaction");
              userTxn.begin();
              ....
              //some SQL statements
              ....
              userTxn.commit();
              }catch (Exception e){
              userTxn.rollback();
              }


              The problem with the above implementation is, it throws a transaction nested exception.

              We have another implementation without the begin and commit(?)..

              UserTransaction userTxn = null;
              try {
              userTxn = (UserTransaction)ctx.lookup("UserTransaction");
              ....
              //some SQL statements
              ....
              }catch (Exception e){
              userTxn.setRollbackonly();
              }

              The above implementation on the otherhand, does not rollback!

              Do you have any idea what is wrong?

              Thanx in advance.