{"record":{"id":"931aaac13167468e","repo":"hibernate/hibernate-orm","slug":"transaction-was-not-properly-begun-started","errorCode":null,"errorMessage":"Transaction was not properly begun/started","messagePattern":"Transaction was not properly begun/started","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/transaction/internal/TransactionImpl.java","lineNumber":105,"sourceCode":"\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}\n\t\telse {\n\t\t\tCORE_LOGGER.committingTransaction();\n\t\t\ttry {\n\t\t\t\tinternalGetTransactionDriverControl().commit();\n\t\t\t}\n\t\t\tcatch (RuntimeException e) {\n\t\t\t\tthrow session.getExceptionConverter().convertCommitException( e );\n\t\t\t}\n\t\t}\n\t}\n\n\t@Nonnull\n\tpublic TransactionDriver internalGetTransactionDriverControl() {\n\t\t// NOTE here to help be a more descriptive NullPointerException\n\t\tif ( transactionDriverControl == null ) {\n\t\t\tthrow new IllegalStateException( \"Transaction was not properly begun/started\" );\n\t\t}\n\t\telse {\n\t\t\treturn transactionDriverControl;\n\t\t}\n\t}\n\n\t@Override\n\tpublic void rollback() {\n\t\tif ( !isActive() && jpaCompliance ) {\n\t\t\tthrow new IllegalStateException( \"rollback() called on inactive transaction (in JPA compliant mode)\" );\n\t\t}\n\n\t\tfinal var status = getStatus();\n\t\tif ( status == TransactionStatus.ROLLED_BACK || status == TransactionStatus.NOT_ACTIVE ) {\n\t\t\t// allow rollback() on completed transaction as noop\n\t\t\tCORE_LOGGER.rollbackCalledOnInactiveTransaction();\n\t\t}\n\t\telse if ( !status.canRollback() ) {","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/transaction/internal/TransactionImpl.java#L87-L123","documentation":"internalGetTransactionDriverControl() returns the TransactionDriver that drives the physical transaction; the field is populated lazily by begin()/isActive() when the session is open. The method exists (per its comment) to turn a confusing NullPointerException into a descriptive IllegalStateException when driver-dependent operations run before the transaction was begun.","triggerScenarios":"Invoking any operation routed through the driver before begin(): commit()/rollback()/markRollbackOnly() paths on a never-begun Transaction, or a setTimeout()/similar call made on a Transaction object obtained but not started.","commonSituations":"Setting a transaction timeout before begin(); unit tests driving TransactionImpl directly without begin(); error handling that calls markRollbackOnly()/rollback() although begin() never ran.","solutions":["Begin the transaction first: use session.beginTransaction() (begins immediately) before setTimeout() or other driver-dependent calls","Check tx.isActive() before touching driver-backed operations","Create the Transaction lazily, right before the work, rather than holding an un-begun reference across the request"],"exampleFix":"// before\nTransaction tx = session.getTransaction();\ntx.setTimeout(30); // driver not created yet -> IllegalStateException\ntx.begin();\n\n// after\nTransaction tx = session.beginTransaction(); // begin() creates the driver\ntx.setTimeout(30);","handlingStrategy":"validation","validationCode":"if (!tx.isActive()) {\n    tx = session.beginTransaction();\n}\ntx.setTimeout(30); // driver exists only after begin","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call session.beginTransaction() (which begins immediately) before driver-dependent calls like setTimeout()","Treat an un-begun Transaction reference as unusable — only isActive() transactions expose driver features"],"tags":["hibernate","transaction","not-started","timeout","internal-api"],"backgroundTag":"transaction-not-started","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}