我試圖完全理解與CMT的JTA分界。我遇到的行爲是,在EJB上只有該方法的第一個@TransactionAttribute受到尊重,而對具有不同@TransactionAttribute註釋的同一個bean的後續方法調用則不是。尊重CMT的JTA事務劃分究竟在哪裏?
例子:
@Stateless
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class Foo implements IFoo {
@EJB
private IBar barBean;
// inherits class transaction annotation of NOT_SUPPORTED
public void doSomething() {
barBean.doAction();
}
}
@Stateless
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class Bar implements IBar {
public void doAction() {
Entity entity = bar.find();
entity.setName("new name");
// fails with EJBException with TransactionRequiredException as cause
save(entity);
}
public Entity find() {
// return some persisted entity.
return em.findById(1);
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Entity save(entity) {
em.persist(em.merge(entity));
em.flush();
}
}
我所看到的行爲是Bar.save()拋出一個TransactionRequiredException。因此,這告訴我在save()上設置的REQUIRED註釋不會創建事務。 REQUIRES_NEW也不起作用。如果我將save()移動到另一個EJB,它將按預期工作。
這是否意味着只有第一個TransactionAttribute註釋被尊重,而不管後續的方法調用與不同的註解值相同?這是一個錯誤還是預期的行爲?我似乎無法找到具體說明這一點的任何文件。我很欣賞這方面的任何見解。
我的堆棧: EJB 3.0, Toplink的要點, GF V2UR2
djna - 謝謝你的迴應。你的解釋是有道理的。我能夠與另一位同事聯繫,他同意你的發言。我感謝你的時間。 – Hoon 2009-10-09 18:08:53