我想记录参与事务的 EJB 方法的错误或成功。我应该把日志放在哪里?据我所知,事务将在我的 doSomething
完成后提交。因此,在这种方法中,我无法确定提交是否成功。这就提出了这个问题。
public class MyEjb {
@Inject
AnotherEjb anotherEjb;
@Inject
LoggerEjb logger;
public void doSomeThing() {
MyBean b = getSomething();
anotherEjb.persistSg(b);
/* logger.log is transaction if of attrubute NOT_SUPPORTED to
ensure separation from caller transaction */
logger.log("Did something successfully.");
}
}
public class AnotherEjb {
@Inject
EntitiyManager em;
public void persistSg(MyBean entity) {
em.persist(entity);
}
}
最佳答案
您尝试过 CDI 的事务观察器吗?
http://docs.jboss.org/weld/reference/latest/en-US/html/events.html#d0e4075
此代码触发一个事件:
@Inject Event<CategoryUpdate> categoryUpdateEvent;
public void someTransactionalMethod() {
CategoryUpdate categoryUpdate = new CategoryUpdate();
categoryUpdateEvent.fire(categoryUpdate);
}
这段代码观察到相同的事件,但只有在事务成功时才会被调用:
public void refreshCategoryTree(@Observes(during = AFTER_SUCCESS) CategoryUpdate event) {
......
}
关于java - 记录来自 EJB 的事务成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11757975/