flowable/flowable-engine · error · ActivitiException
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
Thrown by EntityManagerSessionImpl.close() when EntityManager.close() raises IllegalStateException. This happens for application-managed EntityManagers that are already closed, or container-managed ones that must not be closed by application code. The engine rethrows it as ActivitiException.
Source
Thrown at modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/EntityManagerSessionImpl.java:79
throw new ActivitiException("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 ActivitiException("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
- Set closeEntityManager=false on EntityManagerSessionFactory when the container manages the EntityManager lifecycle
- Avoid closing the EntityManager manually while the engine session still owns it
- Check for duplicate session close calls in custom command interceptors
- Upgrade container integration (e.g. SpringEntityManagerSessionFactory) to manage close correctly
Example fix
// before new EntityManagerSessionFactory(emf, false, true); // engine closes container-managed EM // after new EntityManagerSessionFactory(emf, false, false); // container closes it
Defensive patterns
Strategy: type-guard
Validate before calling
if (entityManager != null && !entityManager.isOpen()) {
throw new IllegalStateException("EntityManager already closed; do not let engine close container-managed EMs");
} Type guard
boolean safeToClose = closeEntityManager && entityManager != null && entityManager.isOpen();
Try / catch
try {
session.close();
} catch (ActivitiException e) {
if (e.getCause() instanceof IllegalStateException) {
// treat as already closed: log and continue
}
} Prevention
- Set jpaCloseEntityManager=false for container-managed EntityManagers
- Avoid double-closing sessions in custom interceptors
- Let Spring/JTA own EM lifecycle in managed environments
When it happens
Trigger: close() invoked with closeEntityManager=true while the EntityManager is already closed; using a container-managed (JTA) EntityManager and letting the engine attempt close; double-close of the session.
Common situations: Spring-managed shared EntityManager passed to EntityManagerSessionFactory with closeEntityManager=true; session closed twice; application closed the EM before the engine session teardown.
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
- Error while flushing EntityManager, illegal state
- Cannot set JPA variable: ${session} not configured
- JPA entity variables can only be used in 'variableValueEqual
- Variables containing a list of JPA entities cannot be used t
AI-assisted analysis of flowable/flowable-engine@d6d39ce1c6 (2026-09-11).
Data as JSON: /api/errors/04236c6c19fbf801.
Report an issue: GitHub.