Reputation:
I have code like in the snippet below. Question is: Why I can't catch the exception in method1, instead doSomeOtherStuff() is called (which should be prevented in case of an exception)
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class EJBBean1 {
@EJB
private EJBBean2 ejb2;
public void method1(Produkt p){
try {
ejb2.method2(p)
doSomeOtherStuff();//is always called
}
catch(Exception e) {
//e is never catched here!!!
}
}
}
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class EJBBean2 {
@PersistenceContext(unitName = "scm")
protected EntityManager em;
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void method2(Produkt p) {
em.merge(p)//Exception rises here (in merge)
}
}
Upvotes: 0
Views: 160
Reputation:
Ok, answer my own question ;-)
Exception does rise in em.merge(), but is not thrown immediately. The JPA container decides when to flush() the merge().
If I change the code a bit:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void method2(Produkt p) {
em.merge(p)
em.flush();//Exception rises here
}
I can catch the Exception immediatly.
Upvotes: 1