{"record":{"id":"b408b865eb139005","repo":"flowable/flowable-engine","slug":"error-while-flushing-entitymanager-illegal-state-b408b8","errorCode":null,"errorMessage":"Error while flushing EntityManager, illegal state","messagePattern":"Error while flushing EntityManager, illegal state","errorType":"exception","errorClass":"ActivitiException","httpStatus":null,"severity":"error","filePath":"modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/EntityManagerSessionImpl.java","lineNumber":57,"sourceCode":"    public EntityManagerSessionImpl(EntityManagerFactory entityManagerFactory, EntityManager entityManager,\n            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 ActivitiException(\"Error while flushing EntityManager, illegal state\", ise);\n            } catch (TransactionRequiredException tre) {\n                throw new ActivitiException(\"Cannot flush EntityManager, an active transaction is required\", tre);\n            } catch (PersistenceException pe) {\n                throw new ActivitiException(\"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":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/EntityManagerSessionImpl.java#L39-L75","documentation":"Thrown by EntityManagerSessionImpl.flush() when the underlying JPA EntityManager.flush() call raises IllegalStateException. The engine wraps it in an ActivitiException to surface a JPA lifecycle problem during variable persistence. It means the EntityManager is in a state where flushing is not allowed (e.g. already closed or no active transaction where required by the persistence context).","triggerScenarios":"Calling flush() when handleTransactions is true but the transaction is not active, or when the EntityManager was closed externally (application-managed EM closed before engine flush), or the EM was created by a container that disallows direct flush in the current context.","commonSituations":"JPA EntityManagerSessionFactory configured with handleTransactions=true but no enclosing transaction (e.g. running a command outside CommandContext transaction management); user code closing the EntityManager before the session flushes; Spring/container-managed EM used in a non-transactional context.","solutions":["Ensure a JPA transaction is active before the flush: wrap the operation in a transaction (UserTransaction / Spring @Transactional) so isTransactionActive() returns true","Check EntityManagerSessionFactory configuration: set handleTransactions to false when the container manages transactions and lifecycle","Do not close the EntityManager yourself if the engine session owns it (closeEntityManager flag)","Inspect the chained IllegalStateException cause for the exact JPA provider message"],"exampleFix":"// before\nentityManagerFactory = new EntityManagerSessionFactory(entityManagerFactory, true);\n// after: let the container manage transactions\nentityManagerFactory = new EntityManagerSessionFactory(entityManagerFactory, false);","handlingStrategy":"try-catch","validationCode":"// before flush\nEntityManagerSession session = Context.getCommandContext().getSession(EntityManagerSession.class);\nif (session != null && session.isTransactionActive()) {\n    session.flush();\n}","typeGuard":"if (entityManager != null && entityManager.isOpen() && entityManager.getTransaction() != null && entityManager.getTransaction().isActive()) {\n    entityManager.flush();\n}","tryCatchPattern":"try {\n    entityManager.flush();\n} catch (ActivitiException e) {\n    if (e.getCause() instanceof IllegalStateException) {\n        // re-open or re-attach EntityManager / start transaction\n    }\n    throw e;\n}","preventionTips":["Always run JPA variable operations inside an active transaction","Let the container manage EM lifecycle when using JTA","Never close an EntityManager the engine session owns"],"tags":["jpa","entitymanager","transaction","activiti"],"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"}