flowable/flowable-engine · error · ActivitiException

Cannot flush EntityManager, an active transaction is require

Error message

Cannot flush EntityManager, an active transaction is required

What it means

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.

Source

Thrown at modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/EntityManagerSessionImpl.java:59

        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 ActivitiException("Error while flushing EntityManager, illegal state", ise);
            } catch (TransactionRequiredException tre) {
                throw new ActivitiException("Cannot flush EntityManager, an active transaction is required", tre);
            } catch (PersistenceException pe) {
                throw new ActivitiException("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. Start a transaction around the operation (JTA UserTransaction.begin() or Spring @Transactional)
  2. Configure EntityManagerSessionFactory with handleTransactions=false if the container/spring handles transactions
  3. Use an extended persistence context or RESOURCE_LOCAL setup where flush without tx is permitted
  4. Check that the CommandContext's transaction propagation reaches the JPA layer

Example fix

// before
entityManagerSession.flush(); // throws outside transaction
// after
try {
    userTransaction.begin();
    entityManagerSession.flush();
    userTransaction.commit();
} catch (Exception e) {
    userTransaction.rollback();
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (entityManager != null && entityManager.getTransaction() != null && !entityManager.getTransaction().isActive()) {
    throw new IllegalStateException("Start a transaction before flushing JPA variables");
}

Type guard

boolean canFlush = entityManager != null && entityManager.getTransaction() != null && entityManager.getTransaction().isActive();

Try / catch

try {
    entityManager.flush();
} catch (TransactionRequiredException | ActivitiException e) {
    userTransaction.begin();
    entityManager.flush();
    userTransaction.commit();
}

Prevention

When it happens

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

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

Related errors


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