{"record":{"id":"b8efb2e06914af68","repo":"flowable/flowable-engine","slug":"error-while-flushing-entitymanager-pe-getmess","errorCode":null,"errorMessage":"Error while flushing EntityManager: \" + pe.getMessage()","messagePattern":"Error while flushing EntityManager: \" \\+ pe\\.getMessage\\(\\)","errorType":"exception","errorClass":"ActivitiException","httpStatus":null,"severity":"error","filePath":"modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/EntityManagerSessionImpl.java","lineNumber":61,"sourceCode":"    }\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();\n            } catch (IllegalStateException ise) {\n                throw new ActivitiException(\"Error while closing EntityManager, may have already been closed or it is container-managed\", ise);","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/EntityManagerSessionImpl.java#L43-L79","documentation":"Thrown by EntityManagerSessionImpl.flush() when the JPA provider raises a generic PersistenceException during EntityManager.flush(). This is the catch-all persistence failure (SQL errors, constraint violations, mapping problems) rethrown as ActivitiException, with the provider message appended.","triggerScenarios":"Any persistence-layer failure during flush: SQL syntax/schema errors, unique constraint violations, detached entity issues, DB connection loss while writing JPA entity variable state.","commonSituations":"Schema out of sync with entity mappings; constraint violation when persisting the JPA entity stored as a process variable; database down or connection pool exhausted during flush.","solutions":["Read the chained PersistenceException cause for the provider-specific root cause (usually a SQLException)","Verify DB schema matches the JPA entity mappings (hbm2ddl validation or migration tool)","Fix constraint/data issues causing the flush failure","Check database connectivity and connection pool health"],"exampleFix":"// before\n// fix at DB level, not code:\n// ERROR: duplicate key value violates unique constraint \"orders_pkey\"\n// after\nALTER TABLE orders ALTER COLUMN id RESTART WITH <max_id + 1>; -- realign sequence","handlingStrategy":"try-catch","validationCode":"// pre-validate entity state\nfor (Object entity : entityList) {\n    if (entityManager.contains(entity) == false && entity.id == null) {\n        throw new IllegalArgumentException(\"Entity not managed and missing id: \" + entity);\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    processEngine.getRuntimeService().setVariable(executionId, \"orders\", orderList);\n} catch (ActivitiException e) {\n    Throwable root = e;\n    while (root.getCause() != null) root = root.getCause();\n    logger.error(\"JPA flush root cause\", root);\n    throw e;\n}","preventionTips":["Keep DB schema in sync with entity mappings (Flyway/Liquibase)","Validate constraint-sensitive data before persisting entities as variables","Monitor DB connectivity and pool exhaustion"],"tags":["jpa","database","flush","persistence"],"backgroundTag":"database-write-failed","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"}