flowable/flowable-engine · error · FlowableException

Cannot flush EntityManager, an active transaction is require

Error message

Cannot flush EntityManager, an active transaction is required

What it means

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.

Source

Thrown at modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/types/EntityManagerSessionImpl.java:58

        this(entityManagerFactory, handleTransactions, closeEntityManager);
        this.entityManager = entityManager;
    }

    public EntityManagerSessionImpl(EntityManagerFactory entityManagerFactory, boolean handleTransactions, boolean closeEntityManager) {
        this.entityManagerFactory = entityManagerFactory;
        this.handleTransactions = handleTransactions;
        this.closeEntityManager = closeEntityManager;
    }

    @Override
    public void flush() {
        if (entityManager != null && (!handleTransactions || isTransactionActive())) {
            try {
                entityManager.flush();
            } catch (IllegalStateException ise) {
                throw new FlowableException("Error while flushing EntityManager, illegal state", ise);
            } catch (TransactionRequiredException tre) {
                throw new FlowableException("Cannot flush EntityManager, an active transaction is required", tre);
            } catch (PersistenceException pe) {
                throw new FlowableException("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();

View on GitHub (pinned to d6d39ce1c6)

Solutions

  1. Enable processEngineConfiguration.setJpaHandleTransactions(true) so Flowable joins/begins the JPA transaction
  2. Run the code inside an active transaction (Spring @Transactional or explicit transaction template)
  3. Use an extended or container-managed persistence context, or call em.getTransaction().begin() before flushing
  4. Check that a platform/JTA transaction manager is correctly wired so the EM sees an active transaction

Example fix

// before
config.setJpaHandleTransactions(false);
// flush -> TransactionRequiredException
// after
config.setJpaHandleTransactions(true);
Defensive patterns

Strategy: try-catch

Validate before calling

if (entityManager != null
        && entityManager.getTransaction() != null
        && !entityManager.getTransaction().isActive()) {
    throw new IllegalStateException("no active JPA transaction; start one before flushing");
}

Try / catch

try {
    session.flush();
} catch (org.flowable.common.engine.api.FlowableException e) {
    if (e.getMessage().contains("active transaction is required")) {
        log.error("flush without transaction; enable jpaHandleTransactions or run inside @Transactional", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of flowable/flowable-engine@d6d39ce1c6 (2026-09-11). Data as JSON: /api/errors/06081e9ae4f02f5a. Report an issue: GitHub.