{"record":{"id":"296bd98d5a9b8d8f","repo":"mybatis/mybatis-3","slug":"cannot-get-configuration-as-factory-method-th-296bd9","errorCode":null,"errorMessage":"Cannot get Configuration as factory method [\" + this.configurationFactory + \"]#[\" + FACTORY_METHOD + \"] didn't return [\" + Configuration.class + \"] but [\" + (configurationObject == null ? \"null\" : configurationObject.getClass()) + \"].\"","messagePattern":"Cannot get Configuration as factory method \\[\" \\+ this\\.configurationFactory \\+ \"\\]#\\[\" \\+ FACTORY_METHOD \\+ \"\\] didn't return \\[\" \\+ Configuration\\.class \\+ \"\\] but \\[\" \\+ \\(configurationObject == null \\? \"null\" : configurationObject\\.getClass\\(\\)\\) \\+ \"\\]\\.\"","errorType":"exception","errorClass":"ExecutorException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/executor/loader/ResultLoaderMap.java","lineNumber":264,"sourceCode":"          });\n        } else {\n          configurationObject = factoryMethod.invoke(null);\n        }\n      } catch (final ExecutorException ex) {\n        throw ex;\n      } catch (final NoSuchMethodException ex) {\n        throw new ExecutorException(\"Cannot get Configuration as factory class [\" + this.configurationFactory\n            + \"] is missing factory method of name [\" + FACTORY_METHOD + \"].\", ex);\n      } catch (final PrivilegedActionException ex) {\n        throw new ExecutorException(\"Cannot get Configuration as factory method [\" + this.configurationFactory + \"]#[\"\n            + FACTORY_METHOD + \"] threw an exception.\", ex.getCause());\n      } catch (final Exception ex) {\n        throw new ExecutorException(\"Cannot get Configuration as factory method [\" + this.configurationFactory + \"]#[\"\n            + FACTORY_METHOD + \"] threw an exception.\", ex);\n      }\n\n      if (!(configurationObject instanceof Configuration)) {\n        throw new ExecutorException(\"Cannot get Configuration as factory method [\" + this.configurationFactory + \"]#[\"\n            + FACTORY_METHOD + \"] didn't return [\" + Configuration.class + \"] but [\"\n            + (configurationObject == null ? \"null\" : configurationObject.getClass()) + \"].\");\n      }\n\n      return Configuration.class.cast(configurationObject);\n    }\n\n    private Log getLogger() {\n      if (this.log == null) {\n        this.log = LogFactory.getLog(this.getClass());\n      }\n      return this.log;\n    }\n  }\n\n  private static final class ClosedExecutor extends BaseExecutor {\n\n    public ClosedExecutor() {","sourceCodeStart":246,"sourceCodeEnd":282,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/executor/loader/ResultLoaderMap.java#L246-L282","documentation":"Thrown when the configurationFactory's static getConfiguration() runs successfully but returns something that is not a MyBatis Configuration instance (including null). MyBatis instanceof-checks the result and reports the actual returned type so the mismatch is obvious.","triggerScenarios":"LoadPair.getConfiguration() invokes the factory and configurationObject fails the instanceof Configuration check — factory returns SqlSessionFactory, a DataSource wrapper, some custom config object, or null.","commonSituations":"Developer returns the SqlSessionFactory instead of its Configuration; factory method returns null because its backing field was never set; overloading getConfiguration() with a different return type that resolves to a non-Configuration implementation.","solutions":["Change the factory to return the Configuration: sqlSessionFactory.getConfiguration()","If it returned null, initialize the backing field before it can be called (lazy-build on first access)","Keep the method signature exactly public static org.apache.ibatis.session.Configuration getConfiguration()"],"exampleFix":"// before\npublic static Configuration getConfiguration() {\n  return App.sqlSessionFactory; // wrong type!\n}\n// after\npublic static Configuration getConfiguration() {\n  return App.sqlSessionFactory.getConfiguration();\n}","handlingStrategy":"type-guard","validationCode":"Object o = MyConfigFactory.getConfiguration();\nif (!(o instanceof Configuration)) throw new IllegalStateException(\"Factory must return Configuration, got: \" + (o == null ? \"null\" : o.getClass()));","typeGuard":"static boolean factoryReturnsConfiguration() { try { return MyConfigFactory.class.getDeclaredMethod(\"getConfiguration\").invoke(null) instanceof Configuration; } catch (ReflectiveOperationException e) { return false; } }","tryCatchPattern":null,"preventionTips":["Return sqlSessionFactory.getConfiguration(), never the factory itself","Boot-time instanceof assertion","Keep the factory signature exactly Configuration getConfiguration()"],"tags":["mybatis","lazy-loading","configuration","type-mismatch"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}