mybatis/mybatis-3 · error · ExecutorException

ResultLoader could not load lazily. DataSource was not conf

Error message

ResultLoader could not load lazily.  DataSource was not configured.

What it means

After confirming an Environment exists, ResultLoader.newExecutor also requires it to carry a DataSource, because a lazy load must open its own connection/transaction. A null DataSource inside the Environment means no connection can be obtained, so this ExecutorException is thrown.

Source

Thrown at src/main/java/org/apache/ibatis/executor/loader/ResultLoader.java:98

    }
    try {
      return localExecutor.query(mappedStatement, parameterObject, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER,
          cacheKey, boundSql);
    } finally {
      if (localExecutor != executor) {
        localExecutor.close(false);
      }
    }
  }

  private Executor newExecutor() {
    final Environment environment = configuration.getEnvironment();
    if (environment == null) {
      throw new ExecutorException("ResultLoader could not load lazily.  Environment was not configured.");
    }
    final DataSource ds = environment.getDataSource();
    if (ds == null) {
      throw new ExecutorException("ResultLoader could not load lazily.  DataSource was not configured.");
    }
    final TransactionFactory transactionFactory = environment.getTransactionFactory();
    final Transaction tx = transactionFactory.newTransaction(ds, null, false);
    return configuration.newExecutor(tx, ExecutorType.SIMPLE);
  }

  public boolean wasNull() {
    return resultObject == null;
  }

}

View on GitHub (pinned to 008069adb1)

Solutions

  1. Pass a real DataSource when constructing the Environment
  2. Verify the DataSource bean/lookup is initialized before the SqlSessionFactory is built
  3. In tests, supply an embedded DataSource (H2) instead of null

Example fix

// before
Environment env = new Environment("dev", new JdbcTransactionFactory(), null);

// after
Environment env = new Environment("dev", new JdbcTransactionFactory(), dataSource);
Defensive patterns

Strategy: validation

Validate before calling

// Verify the Environment carries a DataSource before enabling lazy loading
Environment env = configuration.getEnvironment();
if (env == null || env.getDataSource() == null) {
  throw new IllegalStateException("Lazy loading requires an Environment with a non-null DataSource");
}

Prevention

When it happens

Trigger: new Environment(id, transactionFactory, null) — constructing the Environment with a null DataSource; custom Environment subclasses returning null from getDataSource(); partially initialized test environments.

Common situations: Programmatic configuration where the DataSource wiring was skipped or deferred; copy-pasted Environment construction; mocking the Environment in tests.

Related errors


AI-assisted analysis of mybatis/mybatis-3@008069adb1 (2026-08-14). Data as JSON: /api/errors/33e561b3c91c3dd7. Report an issue: GitHub.