I am referring to container managed transaction attributes in Enterprise Java Beans. I can't think of any use cases where using 'Mandatory' and 'Never' makes sense. Can anyone please explain what cases warrant using these transaction attributes?
Here's my stab on this:
Mandatory: An EJB may be providing some internal function that assumes/relies on a caller's transaction already running, and if it is not, for various reasons, cannot initiate one and so it will throw an EJB error. So the real question here is why would that ever be a requirement and the only scenario I can devise would be one where there may be specific transaction related actions that must be executed when a transaction starts and some EJBs are not equipped for these actions and so are marked mandatory. I suppose you might also use this attribute to ensure a consistent and correctly ordered lock acquisition where a failure to do so could result in a deadlock.
Never: This forces your EJB to throw an exception if a transaction is running when the EJB is invoked, and again, the real question is what sort of scenario would require this. Referring to Mastering EJB Third Edition, Ed Roman asserts that this attribute is useful in reducing client side coding errors by preventing the incorrect assumption that the EJB will participate in an ACID procedure.
Perhaps others will be able to supply more concrete scenarios for these attributes.
i have a case where the mandatory attribute is usefull:
i have a bean, with is called by many applications. depending on the calling application, i want the possibility to join the calling transaction, or run in a new transaction. so i have 2 beans defined, one with RequiresNew and one with Required. The second one can use the Mandatory, to ensure there is an calling transaction, but it is not nessesery.