{"record":{"id":"511897b2f4a564b8","repo":"mybatis/mybatis-3","slug":"error-cannot-commit-no-managed-session-is-star","errorCode":null,"errorMessage":"Error:  Cannot commit.  No managed session is started.","messagePattern":"Error:  Cannot commit\\.  No managed session is started\\.","errorType":"exception","errorClass":"SqlSessionException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/session/SqlSessionManager.java","lineNumber":284,"sourceCode":"      throw new SqlSessionException(\"Error:  Cannot get connection.  No managed session is started.\");\n    }\n    return sqlSession.getConnection();\n  }\n\n  @Override\n  public void clearCache() {\n    final SqlSession sqlSession = localSqlSession.get();\n    if (sqlSession == null) {\n      throw new SqlSessionException(\"Error:  Cannot clear the cache.  No managed session is started.\");\n    }\n    sqlSession.clearCache();\n  }\n\n  @Override\n  public void commit() {\n    final SqlSession sqlSession = localSqlSession.get();\n    if (sqlSession == null) {\n      throw new SqlSessionException(\"Error:  Cannot commit.  No managed session is started.\");\n    }\n    sqlSession.commit();\n  }\n\n  @Override\n  public void commit(boolean force) {\n    final SqlSession sqlSession = localSqlSession.get();\n    if (sqlSession == null) {\n      throw new SqlSessionException(\"Error:  Cannot commit.  No managed session is started.\");\n    }\n    sqlSession.commit(force);\n  }\n\n  @Override\n  public void rollback() {\n    final SqlSession sqlSession = localSqlSession.get();\n    if (sqlSession == null) {\n      throw new SqlSessionException(\"Error:  Cannot rollback.  No managed session is started.\");","sourceCodeStart":266,"sourceCodeEnd":302,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/session/SqlSessionManager.java#L266-L302","documentation":"SqlSessionManager.commit() forwards to the thread-local managed session and throws SqlSessionException 'Cannot commit. No managed session is started.' when localSqlSession is empty. Commit is only meaningful inside the managed-session lifecycle (startManagedSession ... commit ... close); without a started session there is no transaction to commit.","triggerScenarios":"commit() invoked before startManagedSession(); after close() already detached the session; from another thread than the session owner.","commonSituations":"Hand-rolled transaction blocks that skip the start call on an error path; code moved into @Async methods losing the ThreadLocal session; refactoring from SqlSession (factory-opened) to SqlSessionManager without adding startManagedSession.","solutions":["Wrap the work: startManagedSession(); try { ...; commit(); } catch { rollback(); } finally { close(); }.","If you never use managed mode, commit on a factory-opened SqlSession instead of the manager.","For Spring apps, prefer SqlSessionTemplate/DataSourceTransactionManager which handle commit lifecycle."],"exampleFix":"// before\nsqlSessionManager.update(\"insertUser\", u);\nsqlSessionManager.commit(); // throws\n// after\nsqlSessionManager.startManagedSession();\ntry {\n  sqlSessionManager.update(\"insertUser\", u);\n  sqlSessionManager.commit();\n} finally { sqlSessionManager.close(); }","handlingStrategy":"validation","validationCode":"sqlSessionManager.startManagedSession();\ntry { /* work */ sqlSessionManager.commit(); }\nfinally { if (sqlSessionManager.isManagedSessionStarted()) sqlSessionManager.close(); }","typeGuard":null,"tryCatchPattern":"try { commit(); } catch (SqlSessionException e) { /* 'No managed session' -> add startManagedSession before work */ throw e; }","preventionTips":["Standardize the lifecycle template: start -> try work+commit -> catch rollback -> finally close.","Start the session before the try block so commit/rollback are always reachable.","In Spring, prefer SqlSessionTemplate so the platform manages commits."],"tags":["mybatis","session-management","transaction","threadlocal"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}