I'm currently using Java EE to inject my EntityManager into a web app as follows:
@PersistenceContext
EntityManager em;
@Resource
UserTransaction utx;
I have this in a request scoped JSF bean. It works, but it's a pain because to avoid the NoTransactionException I have to wrap every DAO method like so:
public void saveSomething(Obj toSave) {
EntityManager em = getEntityManager();
UserTransaction utx = getTransaction();
try {
utx.begin();
em.persist(toSave);
utx.commit();
} catch(Exception e) {
logger.error("Error saving",e);
try {
utx.rollback();
} catch(Exception ne) {
logger.error("Error saving",ne);
}
return null;
}
}
}
Is there any way to have the container manage the transactions for me in a project like this consisting only of a WAR file?