{"record":{"id":"b55d464f33513b63","repo":"mybatis/mybatis-3","slug":"resultloader-could-not-load-lazily-environment-w","errorCode":null,"errorMessage":"ResultLoader could not load lazily.  Environment was not configured.","messagePattern":"ResultLoader could not load lazily\\.  Environment was not configured\\.","errorType":"exception","errorClass":"ExecutorException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/executor/loader/ResultLoader.java","lineNumber":94,"sourceCode":"  private <E> List<E> selectList() throws SQLException {\n    Executor localExecutor = executor;\n    if (Thread.currentThread().getId() != this.creatorThreadId || localExecutor.isClosed()) {\n      localExecutor = newExecutor();\n    }\n    try {\n      return localExecutor.query(mappedStatement, parameterObject, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER,\n          cacheKey, boundSql);\n    } finally {\n      if (localExecutor != executor) {\n        localExecutor.close(false);\n      }\n    }\n  }\n\n  private Executor newExecutor() {\n    final Environment environment = configuration.getEnvironment();\n    if (environment == null) {\n      throw new ExecutorException(\"ResultLoader could not load lazily.  Environment was not configured.\");\n    }\n    final DataSource ds = environment.getDataSource();\n    if (ds == null) {\n      throw new ExecutorException(\"ResultLoader could not load lazily.  DataSource was not configured.\");\n    }\n    final TransactionFactory transactionFactory = environment.getTransactionFactory();\n    final Transaction tx = transactionFactory.newTransaction(ds, null, false);\n    return configuration.newExecutor(tx, ExecutorType.SIMPLE);\n  }\n\n  public boolean wasNull() {\n    return resultObject == null;\n  }\n\n}\n","sourceCodeStart":76,"sourceCodeEnd":110,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/executor/loader/ResultLoader.java#L76-L110","documentation":"ResultLoader.newExecutor builds a throwaway SIMPLE executor to run the nested query behind a lazy property, using the current Configuration's Environment (DataSource + TransactionFactory). If the Configuration has no Environment (typical when it was built programmatically or deserialized without one), lazy loading cannot open a connection and this ExecutorException is thrown.","triggerScenarios":"Accessing a lazy property when configuration.getEnvironment() returns null — programmatic Configuration without an Environment, a deserialized Configuration that lost it, or a custom SqlSessionFactory setup that skipped environment registration.","commonSituations":"Using new Configuration() directly and building a SqlSessionFactory without setting an Environment; unit tests with hand-built configurations; Spring setups that replace the environment incorrectly.","solutions":["Configure the Environment: configuration.setEnvironment(new Environment(\"id\", transactionFactory, dataSource))","Build the SqlSessionFactory with a SqlSessionFactoryBuilder and a parsed XML that contains <environments default=...>","In Spring, let mybatis-spring create the factory so the Environment is always set"],"exampleFix":"// before\nConfiguration cfg = new Configuration();\nnew SqlSessionFactoryBuilder().build(cfg); // no Environment -> lazy load fails\n\n// after\nDataSource ds = ...;\nEnvironment env = new Environment(\"dev\", new JdbcTransactionFactory(), ds);\ncfg.setEnvironment(env);","handlingStrategy":"validation","validationCode":"// Verify at startup that lazy loading can work\nif (configuration.getEnvironment() == null) {\n  throw new IllegalStateException(\"An Environment (DataSource + TransactionFactory) is required for lazy loading\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always build the SqlSessionFactory from XML with <environments> or set the Environment programmatically","In Spring, use SqlSessionFactoryBean so the Environment is configured for you"],"tags":["lazy-loading","environment","configuration"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}