This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a question related to stateless beans, transactions and transaction attributes.
I have a stateless bean MyBean with two functions - funcA() annotated with a transaction attribute of NEVER and funcB() annotated with a attribute of REQUIRED.
When funcA() gets invoked from some other stateless bean, funcA() runs without a transaction as it is annotated with an attribute of NEVER. But, I see that if funcB() is called from funcA(), even funcB() runs without a transaction. Is this the expected behaviour? Is it because call to funcB() is a direct call from funcA() and hence the container does not care about the transaction attributes present on funcB()? Can some one provide information on this?
One additional observation is that from funcA(), if funcB() is invoked via the local interface of MyBean rather than making a direct call, funcB() indeed runs in a transaction.
Is this the only way to have funcB() running in a transaction when called from funcA()?
If your funcB() call from funcA() is on the same object (like you could use this.funcB() in your code), the EJB container has no way to intercept the call and provide the transaction mechanism around funcB().
The EJB specification says somewhere that transaction annotations (and all interceptors I think) are processed only for calls using an "EJB reference", not for pojo internal calls. That's what you observe using the local interface of your bean.
I really lost hours of debugging on a problem of this kind (REQUIRED_NEW that did not create a new transaction), so now I never forget this
Christian Gossart wrote:
The EJB specification says somewhere that transaction annotations (and all interceptors I think) are processed only for calls using an "EJB reference", not for pojo internal calls.
That's correct, plain Java calls will not introduce EJB semantics. Here's an example on how to invoke a second method from the bean's business method, if you want to introduce the EJB semantics.
Yes, I suspected that the problem was due to the plain java call and made a call through the bean interface to get the required behavior. But I just wanted to get it confirmed. Thanks to both of you guys for the replies and the confirmation.