Solution for Container-Managed Entity Manager:
1) Do not use Persistence.createEntityManagerFactory("UNIT_NAME").createEntityManager();
2) If Entity manager injected in EJB with @PersistenceContext(name = "persistence/UNIT_NAME") private EntityManager em; annotation
EJB, than to find same entity manager in Activiti Engine code use next code:
InitialContext ctx = new InitialContext();
EntityManager em = (EntityManager) ctx
.lookup("java:comp/env/persistence/UNIT_NAME");
I think best place for this lookup in EntityManagerSessionFactory.openSession() .
public Session openSession() {
InitialContext ctx = new InitialContext();
EntityManager entityManager = (EntityManager) ctx
.lookup("java:comp/env/persistence/UNIT_NAME");
return new EntityManagerSessionImpl(null, entityManager, handleTransactions, closeEntityManager);
}
3) Set handleTransactions = false, closeEntityManager = false
Lis