{"record":{"id":"e07fd7e110a91296","repo":"hibernate/hibernate-orm","slug":"no-active-transaction","errorCode":null,"errorMessage":"No active transaction","messagePattern":"No active transaction","errorType":"exception","errorClass":"TransactionRequiredException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java","lineNumber":1148,"sourceCode":"\t\tsessionUseProhibitedDepth--;\n\t}\n\n\tprotected void checkSessionReentrancy() {\n\t\tif ( sessionUseProhibitedDepth > 0 ) {\n\t\t\tthrow new IllegalStateException( \"Session method called from entity lifecycle callback or Interceptor method\" );\n\t\t}\n\t}\n\n\tprotected void checksBeforeQueryCreation() {\n\t\tcheckOpen();\n\t\tcheckTransactionSyncStatus();\n\t}\n\n\t@Override\n\tpublic void prepareForQueryExecution(boolean requiresTxn) {\n\t\tchecksBeforeQueryCreation();\n\t\tif ( requiresTxn && !isTransactionInProgress() ) {\n\t\t\tthrow new TransactionRequiredException( \"No active transaction\" );\n\t\t}\n\t}\n\n\t@Override\n\t@Nullable\n\tpublic Timeout getDefaultTimeout() {\n\t\tfinal var timeoutInMilliseconds = getHintedQueryTimeout();\n\t\treturn timeoutInMilliseconds != null\n\t\t\t\t? Timeouts.interpretMilliSeconds( timeoutInMilliseconds )\n\t\t\t\t: null;\n\t}\n\n\tprotected Integer getHintedQueryTimeout() {\n\t\treturn LegacySpecHelper.getInteger(\n\t\t\t\tHINT_SPEC_QUERY_TIMEOUT,\n\t\t\t\tHINT_JAVAEE_QUERY_TIMEOUT,\n\t\t\t\tthis::getSessionProperty\n\t\t);","sourceCodeStart":1130,"sourceCodeEnd":1166,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java#L1130-L1166","documentation":"prepareForQueryExecution(requiresTxn=true) first checks the session is open and its transaction sync status, then demands an active transaction: requiresTxn && !isTransactionInProgress() throws TransactionRequiredException('No active transaction'). It is the guard for API paths that execute transactional work — notably procedure-call execution and other entry points flagged as requiring a transaction.","triggerScenarios":"Creating/executing a transaction-requiring query path without a transaction: StoredProcedureQuery execution and internal query preparation paths that pass requiresTxn=true, invoked outside beginTransaction()/@Transactional, or on a JTA thread whose transaction never started.","commonSituations":"Calling stored-procedure DML from a non-transactional method; plain Java SE usage where beginTransaction() was dropped in a refactor; Spring self-invocation bypassing the @Transactional proxy; test code calling query APIs directly without a transactional test (missing @Transactional on the test class).","solutions":["Wrap the operation in a transaction: session.beginTransaction() ... commit(), or @Transactional on the method.","In Spring, verify the call crosses the proxy boundary (public method on another bean), not self-invocation.","In tests, annotate the test/method @Transactional (Spring test framework) or use TransactionTemplate."],"exampleFix":"// before\nStoredProcedureQuery q = em.createStoredProcedureQuery(\"recalc_totals\");\nq.execute(); // TransactionRequiredException: No active transaction\n// after\nem.getTransaction().begin();\ntry {\n    q.execute();\n    em.getTransaction().commit();\n} catch (RuntimeException e) {\n    em.getTransaction().rollback();\n    throw e;\n}","handlingStrategy":"validation","validationCode":"if (!session.isTransactionInProgress()) {\n    session.beginTransaction();\n}\nStoredProcedureQuery q = em.createStoredProcedureQuery(\"recalc_totals\");\nq.execute();","typeGuard":null,"tryCatchPattern":"try {\n    q.execute();\n} catch (TransactionRequiredException e) {\n    // only retry when caller forgot the tx; never mask business failures\n    session.beginTransaction();\n    q.execute();\n    session.getTransaction().commit();\n}","preventionTips":["Annotate service methods that execute procedures/mutations with @Transactional by default","Watch for Spring self-invocation bypassing the transactional proxy","In tests, use @Transactional test support instead of bare repository calls"],"tags":["hibernate","transactions","transactionrequiredexception","stored-procedure","orm"],"backgroundTag":"transaction-required","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}