Re: A question about declarative transaction management
Posted By:
Bozidar_Dangubic
Posted On:
Tuesday, October 23, 2001 06:09 AM
your entire question is in regards to what happens when runtime exception occurs during the execution of a method called with tx_required. well, if runtime exception such as NullPointerException occurs during the method execution, the ejb container should automatically wrap the exception into RemoteException and send it over to the client which initiated the call that caused it. the container should also set the transaction for rollback (by calling EJBContext.setRollbackOnly()). once this call happened, no one including the container can commit the transaction so by all purposes this transaction is rolled back. now you asked what if another code or some bean catches runtime exception. well, this is BAD code. you never should catch runtime exceptions. some people make some application exceptions runtime exception so that they do not have to catch them from the client code, but this is a horrible design. runtime exceptions should not be caught. you should never see catch(NullPointerException npEx){....}. but, lets say this is the case and NullPointerException is caught in the method which executes in a transaction. nothing happens. the container is not even aware that this exception occured since it has not been propagated outside the method call. when method is complete, no exceptions propagated out of it so container will just move on. however, code that catches runtime exception can explicitly call EJBContext's.setRollbackOnly() method to mark the transaction for rollback. in this case, runtime exception thrown should be wrapped in EJBException and re-thrown from the catch block.