{"record":{"id":"89ce820910da5ed2","repo":"hibernate/hibernate-orm","slug":"transaction-already-active-in-jpa-compliant-mode","errorCode":null,"errorMessage":"Transaction already active (in JPA compliant mode)","messagePattern":"Transaction already active \\(in JPA compliant mode\\)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/transaction/internal/TransactionImpl.java","lineNumber":71,"sourceCode":"\t\tif ( CORE_LOGGER.isDebugEnabled() && jpaCompliance ) {\n\t\t\tCORE_LOGGER.transactionCreatedInJpaCompliantMode();\n\t\t}\n\t}\n\n\t@Override\n\tpublic void begin() {\n\t\tif ( !session.isOpen() ) {\n\t\t\tthrow new IllegalStateException( \"Cannot begin Transaction on closed Session/EntityManager\" );\n\t\t}\n\n\t\tif ( transactionDriverControl == null ) {\n\t\t\ttransactionDriverControl =\n\t\t\t\t\ttransactionCoordinator.getTransactionDriverControl();\n\t\t}\n\n\t\tif ( isActive() ) {\n\t\t\tif ( jpaCompliance ) {\n\t\t\t\tthrow new IllegalStateException( \"Transaction already active (in JPA compliant mode)\" );\n\t\t\t}\n\t\t\telse if ( !transactionCoordinator.getTransactionCoordinatorBuilder().isJta() ) {\n\t\t\t\tthrow new IllegalStateException( \"Resource-local transaction already active\" );\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tCORE_LOGGER.beginningTransaction();\n\t\t\ttransactionDriverControl.begin();\n\t\t}\n\t}\n\n\t@Override\n\tpublic void commit() {\n\t\t// allow MARKED_ROLLBACK to propagate through to transactionDriverControl\n\t\tif ( !isActive() ) {\n\t\t\t// we have a transaction that is inactive and has not been marked for rollback only\n\t\t\tthrow new IllegalStateException( \"Transaction not successfully started\" );\n\t\t}","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/transaction/internal/TransactionImpl.java#L53-L89","documentation":"TransactionImpl.begin() detects that a transaction is already active. When the session factory runs with JPA transaction compliance enabled (hibernate.jpa.compliance.transaction=true, the default for JPA-bootstrapped factories), re-beginning violates the JPA contract, so an IllegalStateException is thrown instead of joining.","triggerScenarios":"Calling getTransaction().begin() a second time without an intervening commit/rollback on the same EntityManager while jpaCompliance is on; a manual begin() racing with container- or framework-managed transaction start (@Transactional, JTA-bound EM).","commonSituations":"Helper methods that 'ensure' a transaction by calling begin() unconditionally inside nested service calls; mixing manual EM transaction control with Spring/JTA-managed transactions; upgrading native Hibernate code to JPA-compliant settings; retry wrappers that restart logic without resetting the EM.","solutions":["Guard with if (!tx.isActive()) tx.begin() so an existing transaction is joined rather than re-begun","Let the container/framework own transaction demarcation (@Transactional) and delete the manual begin()","Ensure every begin() has a matching commit/rollback path so the next begin() starts clean"],"exampleFix":"// before\nTransaction tx = em.getTransaction();\ntx.begin();\ndoWork();\ntx.begin(); // JPA-compliant mode -> IllegalStateException\n\n// after\nTransaction tx = em.getTransaction();\nif (!tx.isActive()) {\n    tx.begin();\n}\ndoWork();","handlingStrategy":"validation","validationCode":"if (!tx.isActive()) {\n    tx.begin();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Wrap begin() in a single 'ensureTransaction' helper and call it instead of raw begin()","Let @Transactional own demarcation; remove manual begin() from managed beans","Always close the transaction (commit/rollback) before the next unit of work on the same EM"],"tags":["hibernate","transaction","jpa-compliance","double-begin","illegalstate"],"backgroundTag":"transaction-already-active","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}