{"record":{"id":"8a9f6120d18b7a2d","repo":"mybatis/mybatis-3","slug":"error-cannot-clear-the-cache-no-managed-sessio","errorCode":null,"errorMessage":"Error:  Cannot clear the cache.  No managed session is started.","messagePattern":"Error:  Cannot clear the cache\\.  No managed session is started\\.","errorType":"exception","errorClass":"SqlSessionException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/session/SqlSessionManager.java","lineNumber":275,"sourceCode":"  @Override\n  public <T> T getMapper(Class<T> type) {\n    return getConfiguration().getMapper(type, this);\n  }\n\n  @Override\n  public Connection getConnection() {\n    final SqlSession sqlSession = localSqlSession.get();\n    if (sqlSession == null) {\n      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.\");","sourceCodeStart":257,"sourceCodeEnd":293,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/session/SqlSessionManager.java#L257-L293","documentation":"SqlSessionManager.clearCache() delegates to the thread-local managed session; when localSqlSession.get() returns null (no startManagedSession() on this thread) it throws SqlSessionException 'Cannot clear the cache. No managed session is started.' It is a usage-order error, not a cache problem: the local (first-level) cache belongs to a session that was never opened on this thread.","triggerScenarios":"Calling clearCache() before startManagedSession(); calling it after close() (which removes the thread-local); calling from a different thread than the one holding the managed session.","commonSituations":"Attempt to force fresh reads by clearing the first-level cache outside a managed session; test code obtaining SqlSessionManager from Spring and calling lifecycle methods directly without the managed-session wrapper.","solutions":["Open a managed session first: startManagedSession(), then clearCache(), then close() in a finally block.","If you only need uncached reads, open a fresh DefaultSqlSession (SqlSessionFactory.openSession()) per read instead of clearing a shared manager.","Verify thread ownership: the ThreadLocal session exists only on the starting thread."],"exampleFix":"// before\nsqlSessionManager.clearCache(); // throws\n// after\nsqlSessionManager.startManagedSession();\ntry { sqlSessionManager.clearCache(); } finally { sqlSessionManager.close(); }","handlingStrategy":"validation","validationCode":"if (sqlSessionManager.isManagedSessionStarted()) {\n  sqlSessionManager.clearCache();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only call clearCache() inside a startManagedSession/close block on the same thread.","For fresh reads, open a new session rather than clearing a shared manager."],"tags":["mybatis","session-management","cache","threadlocal"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}