I have a stateless session bean and from the method i am calling a POJO which updates 2 different tables. In event of an exception i am calling the setRollBackOnly() method on the context in the ejb. The problem is that if an exception occurs during the update of the 2nd table the rollback does not haapens as the data gets updated in the 1st table whereas it shud not get updated as an exception has occured.
It works fine when i specify <trans-attribute>Required</trans-attribute> for the method. But i was thinking that setRollBackOnly() will be enough to rollback. Whats the importance of each of these 2 while doing a rollback?
The setRollbackOnly method only works in a transaction. If you fail to specify <trans-attribute>Required</trans-attribute>, the container will assign a default value, maybe NotSupported. In other words, the method will run in an unspecified transaction context which in your case means not running in a transaction. If the caller is part of a transaction, then the caller's transaction is suspended. If the EJB method fails, there will be no effect on the caller's transaction, and no rollback will occur.
Your case is a good example why transaction attributes must be specified for the appropriate methods of a CMT bean, as you may otherwise get an unwanted default attribute.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Whose rules are you playing by? This tiny ad doesn't respect those rules: