I’d like to log the error or the success of an EJB method that is participating in a transaction. Where shall I put the logging? As far as I know the transaction will be committed after my doSomething has finished. So in that method I cannot be sure that the commit would be successful or not. That raised this question.
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);
}
}
Have you tried CDI’s transactional observers?
http://docs.jboss.org/weld/reference/latest/en-US/html/events.html#d0e4075
This code fires an event:
And this piece of code observes the same event, but will be called only if the transaction succeeds: