flowable/flowable-engine · error · FlowableException
Error while closing EntityManager, may have already been clo
Error message
Error while closing EntityManager, may have already been closed or it is container-managed
What it means
At session close, Flowable calls entityManager.close() only if the EntityManager is application-managed and still open. If the JPA provider throws IllegalStateException during close ( EntityManager already closed, or container-managed so the application must not close it), this FlowableException is thrown.
Source
Thrown at modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/types/EntityManagerSessionImpl.java:78
throw new FlowableException("Error while flushing EntityManager: " + pe.getMessage(), pe);
}
}
}
protected boolean isTransactionActive() {
if (handleTransactions && entityManager.getTransaction() != null) {
return entityManager.getTransaction().isActive();
}
return false;
}
@Override
public void close() {
if (closeEntityManager && entityManager != null && entityManager.isOpen()) {
try {
entityManager.close();
} catch (IllegalStateException ise) {
throw new FlowableException("Error while closing EntityManager, may have already been closed or it is container-managed", ise);
}
}
}
@Override
public EntityManager getEntityManager() {
if (entityManager == null) {
entityManager = getEntityManagerFactory().createEntityManager();
if (handleTransactions) {
// Add transaction listeners, if transactions should be handled
TransactionListener jpaTransactionCommitListener = new TransactionListener() {
@Override
public void execute(CommandContext commandContext) {
if (isTransactionActive()) {
entityManager.getTransaction().commit();
}
}View on GitHub (pinned to d6d39ce1c6)
Solutions
- Configure Flowable to NOT close the EntityManager when it is container-managed (set closeEntityManager false in EntityManagerSessionFactory)
- Ensure the EntityManagerFactory passed to Flowable creates application-managed EntityManagers
- Check for code paths that close the EntityManager before the session close runs
- Review Spring/EE config so the same EM isn't shared and closed elsewhere
Example fix
// before
EntityManagerFactory emf = Persistence.createEntityManagerFactory("containerManagedUnit");
new EntityManagerSessionFactory(emf, true, true); // handleTransactions, closeEntityManager=true
// after
new EntityManagerSessionFactory(emf, true, false); // let the container manage EM lifecycle Defensive patterns
Strategy: try-catch
Validate before calling
// configuration check before engine use
if (usingContainerManagedEm) {
assert engineConfigJpaFactory.isCloseEntityManager() == false;
} Type guard
boolean safeToClose(EntityManager em) { return em != null && em.isOpen(); } Try / catch
try { session.close(); } catch (FlowableException e) { log.warn("EntityManager close skipped: {}", e.getMessage()); } // close-time failure is non-fatal to business logic Prevention
- Set closeEntityManager=false when the EntityManager is container-managed (Spring @PersistenceContext, Java EE)
- Never manually close EntityManagers owned by Flowable sessions
- Use one EntityManagerFactory per engine and let Flowable own its lifecycle
When it happens
Trigger: EntityManagerSession.close() invoked while the underlying EntityManager is already closed or is container-managed (e.g. injected via @PersistenceContext in a Java EE/Spring container) and closeEntityManager was configured true.
Common situations: Mixing container-managed persistence (Spring/Java EE) with Flowable's JPA variable handling; double-closing a session; custom EntityManagerFactory configuration that pre-closes or shares EMs across threads.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- Error while closing EntityManager, may have already been clo
- Cannot complete process instance. Parent process instance ${
- Cannot complete case task. Parent process instance ${executi
- lazy loading outside command context for " + this
- The ${task} cannot be deleted because is part of a running p
AI-assisted analysis of flowable/flowable-engine@d6d39ce1c6 (2026-09-11).
Data as JSON: /api/errors/32d9bd693bbbda92.
Report an issue: GitHub.