To rollback or not rollback

Discussions

EJB programming & troubleshooting: To rollback or not rollback

  1. To rollback or not rollback (3 messages)

    Hi,

    I have a client side code that starts a transaction and calls methods in a session bean.

    Currently my code looks like this:

    try {
        UserTransaction utx = HomeFinder.getUserTransaction();

        utx.begin();
        // calls session bean method
        utx.commit();
    } catch(Exception e) {
        //rethrow
    }

    Whenever there is an exception within the call to the session bean method, the catch block will execute and the utx.commit will not. If that is the case, then should I do a rollback in the catch block? If yes, what does rollback do such that simply not commiting is not enough?

    thanks.

    mgop

    Threaded Messages (3)

  2. To rollback or not rollback[ Go to top ]


    Yes, you have to rollback in the catch block. I was wondering why you are not using container managed transaction for your beans. Is there any specific reason?

    Pranab Ghosh
    Independent Consultant
  3. To rollback or not rollback[ Go to top ]

    Yes, it is because between utx.begin() and utx.commit() I make several calls to the session bean, and the whole operation is one transaction. If I use container managed transaction, then each call to the session bean is a transaction by itself.

    Thanks for your answer!
  4. To rollback or not rollback[ Go to top ]

    you could always set the transaction attribute of the lower level seeion methods to SUPPORTS so that they get included in a single transaction scope.
    ie
    Set transaction attributes at method level so that
     A -> begin transaction
      -> B -> Session calls in the same transaction as A
                 (transaction attribute Supports)
      -> C -> Session calls in the same transaction as A
                 (transaction attribute Supports)
      -> end transaction
    Neeraj