{"record":{"id":"1c10e2ad9fcb516b","repo":"hibernate/hibernate-orm","slug":"cannot-begin-transaction-on-closed-session-entitym","errorCode":null,"errorMessage":"Cannot begin Transaction on closed Session/EntityManager","messagePattern":"Cannot begin Transaction on closed Session/EntityManager","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/transaction/internal/TransactionImpl.java","lineNumber":61,"sourceCode":"\t\t\t\t\t\t.isJpaTransactionComplianceEnabled();\n\n\t\tif ( session.isOpen() && transactionCoordinator.isActive() ) {\n\t\t\ttransactionDriverControl =\n\t\t\t\t\ttransactionCoordinator.getTransactionDriverControl();\n\t\t}\n\t\telse {\n\t\t\tCORE_LOGGER.transactionCreatedOnClosedSession();\n\t\t}\n\n\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();","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/transaction/internal/TransactionImpl.java#L43-L79","documentation":"TransactionImpl.begin() first checks session.isOpen(); a closed Session/EntityManager can no longer coordinate a physical transaction, so begin() throws IllegalStateException instead of resurrecting it. This mirrors the JPA rule that a closed EntityManager is unusable.","triggerScenarios":"em.getTransaction().begin() or session.beginTransaction() after em.close()/session.close(); the same call on a session closed by a container filter (Open EntityManager in View), an earlier exception, or another thread sharing the session.","commonSituations":"An EntityManager cached in a field, HttpSession attribute, or singleton bean reused after the owning request closed it; closing order bugs in @PreDestroy or try-with-resources; tests that close the EM in setup but keep using it; a shared session accessed concurrently so one thread closes it mid-use.","solutions":["Get a fresh EntityManager/Session from the factory instead of reusing the closed instance","Check session.isOpen() before begin() and reopen or fail with a clear message when closed","Fix lifecycle ownership: the component that begins the transaction should also own (and close) the session"],"exampleFix":"// before\nEntityManager em = cachedEm; // may already be closed\nem.getTransaction().begin(); // IllegalStateException\n\n// after\nif (!em.isOpen()) {\n    em = emf.createEntityManager();\n}\nem.getTransaction().begin();","handlingStrategy":"validation","validationCode":"if (!session.isOpen()) {\n    session = sessionFactory.openSession(); // or emf.createEntityManager()\n}\nsession.beginTransaction();","typeGuard":null,"tryCatchPattern":"try {\n    em.getTransaction().begin();\n} catch (IllegalStateException e) {\n    if (String.valueOf(e.getMessage()).contains(\"closed Session\")) {\n        em = emf.createEntityManager();\n        em.getTransaction().begin();\n    } else {\n        throw e;\n    }\n}","preventionTips":["Never cache EntityManager/Session in long-lived fields; create per unit of work","Check isOpen() at hand-off boundaries (e.g. receiving an EM from a filter)","Keep session ownership single-threaded and single-owner"],"tags":["hibernate","transaction","closed-session","lifecycle","entitymanager"],"backgroundTag":"operation-on-closed-session","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}