0
votes

My application is deployed on WildFly 8.2.

I'm processing a batch.

Method A in Session EJB has required new annotation.

Inside of it I am calling another method, Method B, on the same EJB with another requires new annotation.

This Method B throws runtime exception. Method A catch it and continues.

But next line which deals with JPA update gives transaction required exception.

Basically I don't know why Method B with Required New annotation affects Method A.

It is entity manager on JPA Dao loosing transaction.

How can I make Method B run in isolation and only rollback Method B if exception happened in it.

1
If you invoke another method from the same EJB, a transaction attribute is not taken into account. If you want a new transaction in method B move it into new EJB. For now method A and method B share the same transaction. - Geinmachi
Thanks, I realized it now as S.Stavreva suggested I will use sessionContext.getBusinessObject(Object.class). - user330026

1 Answers

3
votes

You can use the session context to call the methodB via the EJB container, so the transaction annotation can take effect.

Your EJB should look something like this:

@Resource
SessionContext sessionCtx;
.
.
.
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void methodA() {
    <Your-EJB-Interface> ejbObject = sessionCtx.getBusinessObject(<Your-EJB-Interface>.class);
     ejbObject.methodB();
}

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void methodB....