0

For the given class bellow, why the transaction scope is the same from the caller method ? And why it only works when i call, for example someBeanInstance.A() from another class ?

@Stateless
public class X {

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void A() {
        B();
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    //or @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public void B() {
        // here the transaction is the same
        // the annotation has no effect
    }
}
Josh
  • 154
  • 1
  • 9
  • You probably need to refactor or use AspectJ: http://stackoverflow.com/questions/3423972/spring-transaction-method-call-by-the-method-within-the-same-class-does-not-wo?rq=1 – Thilo Sep 01 '14 at 04:57
  • 1
    Im not using Spring, just pure Java EE. – Josh Sep 01 '14 at 04:59
  • 1
    Then you probably don't have the AspectJ option. The problem with proxies seems to be the same, at least it was in 2009: http://stackoverflow.com/a/427582/14955 Calls on `this` won't get the EJB goodies. – Thilo Sep 01 '14 at 05:03
  • The link was very helpful, thanks. Do you know some workaround for this case ? It would be awful create separated classes just to be proxied on every call – Josh Sep 01 '14 at 05:24
  • 1
    In that link, they say "the fix is to either inject a reference to the same object (for stateless or singleton), or to use SessionContext.getEJBLocalObject, getEJBObject, or getBusinessObject (possibly with getInvokedBusinessInterface) to get a proxy to the current object." – Thilo Sep 01 '14 at 06:58

0 Answers0