EJB 3.1 TransactionAttributeType.REQUIRES_NEW and setRollbackOnly

Please help me understand transactions in EJB 3.1. I am using GlassFish v3 and have the following situation:

@Stateless @LocalBean public class BeanA { @Inject BeanB bean; /* which has no TransactionAttribute set */ @Resource SessionContext context; public void run() { ... for (...) { process(someValue); } } @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void process(String someValue) { try { SomeEntity entity = bean.getEntity(someValue); entity.setSomeProperty("anotherValue"); ... } catch(CustomException e) { this.context.setRollbackOnly(); } } } 

BeanA.run is called from the servlet. I want to consider each iteration as a separate transaction. I thought using TransactionAttributeType.REQUIRES_NEW was aware of this, but I get javax.ejb.EJBTransactionRolledbackException on subsequent iterations in beanB after calling setRollbackOnly. The strange thing though, when I move everything except run () to a new BeanC and beanC.process is called, it works. What am I missing? Can anyone shed some light on why this works the way it is?

Edit: think about it: is it because the container does not intercept method calls within the same EJB? (which would seem reasonable)

Edit 2: Yes, I found the answer here: EJB transactions in local method calls (I had to know the answer to find it :))

+4
source share
1 answer

Found the answer here: EJB transactions in local method calls

In short: the container does not intercept calls to local methods, so setRollbackOnly is marked with a single rollback transaction, explaining the exceptions.

+4
source

Source: https://habr.com/ru/post/1396586/


All Articles