{"record":{"id":"b916f91ea9e7b1d2","repo":"flowable/flowable-engine","slug":"cannot-flush-entitymanager-an-active-transaction-b916f9","errorCode":null,"errorMessage":"Cannot flush EntityManager, an active transaction is required","messagePattern":"Cannot flush EntityManager, an active transaction is required","errorType":"exception","errorClass":"ActivitiException","httpStatus":null,"severity":"error","filePath":"modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/EntityManagerSessionImpl.java","lineNumber":59,"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 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()) {\n            try {\n                entityManager.close();","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/EntityManagerSessionImpl.java#L41-L77","documentation":"Thrown by EntityManagerSessionImpl.flush() when the JPA provider raises TransactionRequiredException during EntityManager.flush(). Some persistence contexts (transaction-scoped, joins configured) require an active transaction for flush. The engine rethrows it as ActivitiException with this explicit message.","triggerScenarios":"Flushing a transaction-scoped persistence context outside a transaction; handleTransactions=true with no active UserTransaction; JPA provider (e.g. Hibernate/EclipseLink with JTA) requiring a transaction for flush.","commonSituations":"Running engine commands without an enclosing transaction while JPA variables are persisted; JTA data source configured but no user transaction started; test setups without transaction management.","solutions":["Start a transaction around the operation (JTA UserTransaction.begin() or Spring @Transactional)","Configure EntityManagerSessionFactory with handleTransactions=false if the container/spring handles transactions","Use an extended persistence context or RESOURCE_LOCAL setup where flush without tx is permitted","Check that the CommandContext's transaction propagation reaches the JPA layer"],"exampleFix":"// before\nentityManagerSession.flush(); // throws outside transaction\n// after\ntry {\n    userTransaction.begin();\n    entityManagerSession.flush();\n    userTransaction.commit();\n} catch (Exception e) {\n    userTransaction.rollback();\n}","handlingStrategy":"try-catch","validationCode":"if (entityManager != null && entityManager.getTransaction() != null && !entityManager.getTransaction().isActive()) {\n    throw new IllegalStateException(\"Start a transaction before flushing JPA variables\");\n}","typeGuard":"boolean canFlush = entityManager != null && entityManager.getTransaction() != null && entityManager.getTransaction().isActive();","tryCatchPattern":"try {\n    entityManager.flush();\n} catch (TransactionRequiredException | ActivitiException e) {\n    userTransaction.begin();\n    entityManager.flush();\n    userTransaction.commit();\n}","preventionTips":["Set jpaHandleTransaction correctly for your environment (Spring/JTA vs standalone)","Wrap process variable writes in @Transactional or UserTransaction","Use extended persistence contexts if flush-without-tx is needed"],"tags":["jpa","transaction","flush","activiti"],"backgroundTag":"authentication-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"}