Transactions
All EJB are transactional. If they start in a transaction they automatically are configured inside the transaction. A simple way to manage the transactions is through the SessionSynchronization
interface. Here's a stateful sample implementing it:
@Stateful(name = "stateEngineRemote") publicclass StateEngineRemoteBean implements SessionSynchronization { ... @Override publicvoid afterBegin() throws EJBException, RemoteException { logger.info("the bean is begun"); } @Override publicvoid beforeCompletion() throws EJBException, RemoteException { logger.info("the bean is completing"); } @Override publicvoid afterCompletion(booleancommitted) throws EJBException, RemoteException { logger.info("the bean is completed"); } ...}
Here's a transactional client that invokes the EJB:
@Inject private UserTransaction userTransaction; @EJB private StateEngineLocal stateEngineLocal ... try { userTransaction.begin(); stateEngineLocal.go(1); userTransaction...