{"record":{"id":"2f5e8b979613a42b","repo":"flowable/flowable-engine","slug":"error-while-flushing-entitymanager-illegal-state","errorCode":null,"errorMessage":"Error while flushing EntityManager, illegal state","messagePattern":"Error while flushing EntityManager, illegal state","errorType":"exception","errorClass":"FlowableException","httpStatus":null,"severity":"error","filePath":"modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/types/EntityManagerSessionImpl.java","lineNumber":56,"sourceCode":"\n    public EntityManagerSessionImpl(EntityManagerFactory entityManagerFactory, EntityManager entityManager, boolean handleTransactions, boolean closeEntityManager) {\n        this(entityManagerFactory, handleTransactions, closeEntityManager);\n        this.entityManager = entityManager;\n    }\n\n    public EntityManagerSessionImpl(EntityManagerFactory entityManagerFactory, boolean handleTransactions, boolean closeEntityManager) {\n        this.entityManagerFactory = entityManagerFactory;\n        this.handleTransactions = handleTransactions;\n        this.closeEntityManager = closeEntityManager;\n    }\n\n    @Override\n    public void flush() {\n        if (entityManager != null && (!handleTransactions || isTransactionActive())) {\n            try {\n                entityManager.flush();\n            } catch (IllegalStateException ise) {\n                throw new FlowableException(\"Error while flushing EntityManager, illegal state\", ise);\n            } catch (TransactionRequiredException tre) {\n                throw new FlowableException(\"Cannot flush EntityManager, an active transaction is required\", tre);\n            } catch (PersistenceException pe) {\n                throw new FlowableException(\"Error while flushing EntityManager: \" + pe.getMessage(), pe);\n            }\n        }\n    }\n\n    protected boolean isTransactionActive() {\n        if (handleTransactions && entityManager.getTransaction() != null) {\n            return entityManager.getTransaction().isActive();\n        }\n        return false;\n    }\n\n    @Override\n    public void close() {\n        if (closeEntityManager && entityManager != null && entityManager.isOpen()) {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/types/EntityManagerSessionImpl.java#L38-L74","documentation":"EntityManagerSessionImpl.flush() calls entityManager.flush() and wraps failures. When the JPA provider throws IllegalStateException (EntityManager in wrong state, e.g. already closed or not joined to a transaction), Flowable rethrows it as FlowableException 'Error while flushing EntityManager, illegal state' with the original as cause.","triggerScenarios":"Session flush at command end while the EntityManager is closed or in an invalid lifecycle state; flushing an application-managed EntityManager that was closed manually; using the EM outside its intended transaction scope while handleTransactions semantics expect a valid state.","commonSituations":"Closing the EntityManager in application code before the Flowable command completes; mixing application-managed and container-managed persistence contexts; transaction infrastructure misconfiguration causing the EM to be detached/closed at flush time.","solutions":["Do not close or clear the EntityManager yourself when it is managed by the Flowable/JPA session; let the session close it","Ensure flush is called while the EM is joined to an active transaction (check isTransactionActive before manual flush)","Align jpaHandleTransactions/jpaCloseEntityManager settings with your transaction manager configuration","Inspect the cause (ise) to identify which EM lifecycle rule was violated and fix the calling code"],"exampleFix":"// before\nentityManager.close(); // closed too early\n// flowable session flush -> IllegalStateException\n// after\n// let the session manage lifecycle; no manual close\nengineConfig.setJpaCloseEntityManager(true);","handlingStrategy":"try-catch","validationCode":"if (entityManager != null && !entityManager.isOpen()) {\n    throw new IllegalStateException(\"EntityManager already closed; do not close it manually before the command completes\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    session.flush();\n} catch (org.flowable.common.engine.api.FlowableException e) {\n    if (e.getMessage().contains(\"illegal state\") && e.getCause() instanceof IllegalStateException) {\n        log.error(\"EntityManager in invalid state at flush; check lifecycle/close handling\", e.getCause());\n    }\n    throw e;\n}","preventionTips":["Let the JPA session/Flowable manage EntityManager lifecycle; avoid manual close/clear","Match jpaHandleTransactions and jpaCloseEntityManager settings to your transaction manager","Investigate the cause chain — the wrapped IllegalStateException names the violated EM state rule"],"tags":["java","flowable","jpa","flush","transaction"],"backgroundTag":"invalid-state-transition","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}