A question about declarative transaction management
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   ramesh_malvey
Posted On:   Monday, October 22, 2001 05:25 PM

Hi, I have a basic question about the declarative transaction management. I have a table called Account in the database which contains account number and amount.I have created an entity bean,called an account entity bean for this table.Next,I would like to have a Stateful session bean which contains two methods,one for deposit and the other for withdrawal.I want to use a JSP as a client for EJB.This JSP will obtain a remote reference for the stateful session bean.Then,it will invoke the deposit method of the stateful session bean.This deposit method,will in turn obtain the remote reference of the account entity bean and sets the amount.Next, I will call the withdraw method of the stateful session bean and try to withdraw beyond the amount    More>>

Hi,


I have a basic question about the declarative transaction management.
I have a table called Account in the database which contains account number and amount.I have created an entity bean,called an account entity bean for this table.Next,I would like to have a Stateful session bean which contains two methods,one for deposit and the other for withdrawal.I want to use a JSP as a client for EJB.This JSP will obtain a remote reference for the stateful session bean.Then,it will invoke the deposit method of the stateful session bean.This deposit method,will in turn obtain the remote reference of the account entity bean and sets the amount.Next, I will call the withdraw method of the stateful session bean and try to withdraw beyond the amount limit.In this case,the method throws a runtime exception.Supposing that I have given the TX_REQUIRED attribute for both the methods of the stateful session bean,will it rollback the transaction automatically and restores the amount value in the database to its previous state i.e. the value it had before a call to the deposit method ?
What if this runtime exception is explicitly caught? will the transaction still be rolled back ?




Suppose,I have a method in Stateful session bean(first ssb) which in turn calls two other methods of another stateful session bean(second ssb) out of which one throws a runtime exception and the method of the first stateful session bean explicitly catches this exception and does the processing. In this case whether the transaction will still be rolled back assuming that all the methods have a TX_Required attribute.




Please help me in understanding the declarative transaction management.

   <<Less

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.
About | Sitemap | Contact