{"record":{"id":"15f9ed95cbc513bb","repo":"mybatis/mybatis-3","slug":"error-cannot-get-connection-no-managed-session","errorCode":null,"errorMessage":"Error:  Cannot get connection.  No managed session is started.","messagePattern":"Error:  Cannot get connection\\.  No managed session is started\\.","errorType":"exception","errorClass":"SqlSessionException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/session/SqlSessionManager.java","lineNumber":266,"sourceCode":"  public int delete(String statement) {\n    return sqlSessionProxy.delete(statement);\n  }\n\n  @Override\n  public int delete(String statement, Object parameter) {\n    return sqlSessionProxy.delete(statement, parameter);\n  }\n\n  @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.\");","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/session/SqlSessionManager.java#L248-L284","documentation":"SqlSessionManager delegates session lifecycle methods to a thread-local SqlSession established by startManagedSession(). getConnection() checks localSqlSession; if no managed session was started on this thread it throws SqlSessionException 'Cannot get connection. No managed session is started.' It means you used the manager's manual/managed mode API without opening a managed session first (or from a different thread than the one that started it).","triggerScenarios":"Calling SqlSessionManager.getConnection() before startManagedSession() on the same thread; starting the managed session in one thread (e.g. servlet filter) and calling getConnection() in another (e.g. async task) since ThreadLocal does not propagate; calling after close() removed the thread-local.","commonSituations":"Integrating SqlSessionManager with custom transaction code and forgetting the start call in one code path; moving work to an Executor/thread pool where the ThreadLocal session is absent; filter ordering so the DAO runs before the session-start filter.","solutions":["Call sqlSessionManager.startManagedSession() (optionally with ExecutorType/autoCommit) before any lifecycle call on that thread.","Ensure start and use happen on the same thread; for async work, start a fresh managed session inside the worker thread.","Prefer the mapper proxy style (getMapper without managed session) so the manager opens/closes sessions per operation automatically."],"exampleFix":"// before\nConnection c = sqlSessionManager.getConnection(); // throws\n// after\nsqlSessionManager.startManagedSession();\ntry {\n  Connection c = sqlSessionManager.getConnection();\n} finally {\n  sqlSessionManager.close();\n}","handlingStrategy":"validation","validationCode":"if (!sqlSessionManager.isManagedSessionStarted()) {\n  sqlSessionManager.startManagedSession();\n}\nConnection c = sqlSessionManager.getConnection();","typeGuard":null,"tryCatchPattern":"try { ... } catch (SqlSessionException e) { /* check 'No managed session is started' -> fix lifecycle */ throw e; }","preventionTips":["Encapsulate the managed-session lifecycle in one filter/interceptor template method.","Check isManagedSessionStarted() before lifecycle calls.","Never share a managed session across threads; ThreadLocal does not propagate."],"tags":["mybatis","session-management","threadlocal","sql-session-exception"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}