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; @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 :))
source share