{"record":{"id":"06081e9ae4f02f5a","repo":"flowable/flowable-engine","slug":"cannot-flush-entitymanager-an-active-transaction","errorCode":null,"errorMessage":"Cannot flush EntityManager, an active transaction is required","messagePattern":"Cannot flush EntityManager, an active transaction is required","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":58,"sourceCode":"        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()) {\n            try {\n                entityManager.close();","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/types/EntityManagerSessionImpl.java#L40-L76","documentation":"EntityManagerSessionImpl.flush() catches jakarta.persistence.TransactionRequiredException from entityManager.flush() and rethrows FlowableException 'Cannot flush EntityManager, an active transaction is required'. This happens when flushing a transaction-scoped persistence context without an active JPA transaction.","triggerScenarios":"Command completes and the session flushes while no JPA transaction is active (jpaHandleTransactions=false but a transactional EM is used, or no transaction was started); calling flush on an EM with no begin()'ed transaction.","commonSituations":"Misconfigured jpaHandleTransactions flag (disabled while using RESOURCE_LOCAL transaction-scoped EM); missing @Transactional / no Spring transaction around the Flowable command; flushing outside transaction boundaries in tests.","solutions":["Enable processEngineConfiguration.setJpaHandleTransactions(true) so Flowable joins/begins the JPA transaction","Run the code inside an active transaction (Spring @Transactional or explicit transaction template)","Use an extended or container-managed persistence context, or call em.getTransaction().begin() before flushing","Check that a platform/JTA transaction manager is correctly wired so the EM sees an active transaction"],"exampleFix":"// before\nconfig.setJpaHandleTransactions(false);\n// flush -> TransactionRequiredException\n// after\nconfig.setJpaHandleTransactions(true);","handlingStrategy":"try-catch","validationCode":"if (entityManager != null\n        && entityManager.getTransaction() != null\n        && !entityManager.getTransaction().isActive()) {\n    throw new IllegalStateException(\"no active JPA transaction; start one before flushing\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    session.flush();\n} catch (org.flowable.common.engine.api.FlowableException e) {\n    if (e.getMessage().contains(\"active transaction is required\")) {\n        log.error(\"flush without transaction; enable jpaHandleTransactions or run inside @Transactional\", e);\n    }\n    throw e;\n}","preventionTips":["Set jpaHandleTransactions(true) when using transaction-scoped (RESOURCE_LOCAL) EntityManagers","Run Flowable commands inside an active transaction (Spring @Transactional / transaction template)","In tests, begin a transaction before flushing the persistence context"],"tags":["java","flowable","jpa","flush","transaction"],"backgroundTag":"transaction-required","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"}