I have such code :
public void createTerminal(TerminalDo terminalDo) {
final EntityManagerFactory emf = JpaFactory.getEntityManagerFactory();
final EntityManager em = emf.createEntityManager();
EntityTransaction etx = null;
try {
etx = em.getTransaction();
etx.begin();
// persist method make the passed Object stored in the persistence
// context
em.persist(terminalDo);
etx.commit();
} catch (Exception e) {
if (etx != null && etx.isActive())
etx.rollback();
e.printStackTrace();
} finally {
em.close();
}
}
This is a transactional method. I don't know if it's possible to create another transactional method calling this already transactional one, then nesting an entity-managed transaction into another one, and is it a good practice ?
public void makeAllThat(TerminalDo terminalDo, ApplicationDo applicationDo) {
final EntityManagerFactory emf = JpaFactory.getEntityManagerFactory();
final EntityManager em = emf.createEntityManager();
EntityTransaction etx = null;
try {
etx = em.getTransaction();
etx.begin();
createTerminal(terminalDo);
createApplication(applicationDo);
//do many other transactions...
.....
etx.commit();
} catch (Exception e) {
if (etx != null && etx.isActive())
etx.rollback();
e.printStackTrace();
} finally {
em.close();
}
}