mybatis/mybatis-3 · error · ExecutorException

Cannot get Configuration as factory method [" + this.configu

Error message

Cannot get Configuration as factory method [" + this.configurationFactory + "]#[" + FACTORY_METHOD + "] didn't return [" + Configuration.class + "] but [" + (configurationObject == null ? "null" : configurationObject.getClass()) + "]."

What it means

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.

Source

Thrown at src/main/java/org/apache/ibatis/executor/loader/ResultLoaderMap.java:264

          });
        } else {
          configurationObject = factoryMethod.invoke(null);
        }
      } catch (final ExecutorException ex) {
        throw ex;
      } catch (final NoSuchMethodException ex) {
        throw new ExecutorException("Cannot get Configuration as factory class [" + this.configurationFactory
            + "] is missing factory method of name [" + FACTORY_METHOD + "].", ex);
      } catch (final PrivilegedActionException ex) {
        throw new ExecutorException("Cannot get Configuration as factory method [" + this.configurationFactory + "]#["
            + FACTORY_METHOD + "] threw an exception.", ex.getCause());
      } catch (final Exception ex) {
        throw new ExecutorException("Cannot get Configuration as factory method [" + this.configurationFactory + "]#["
            + FACTORY_METHOD + "] threw an exception.", ex);
      }

      if (!(configurationObject instanceof Configuration)) {
        throw new ExecutorException("Cannot get Configuration as factory method [" + this.configurationFactory + "]#["
            + FACTORY_METHOD + "] didn't return [" + Configuration.class + "] but ["
            + (configurationObject == null ? "null" : configurationObject.getClass()) + "].");
      }

      return Configuration.class.cast(configurationObject);
    }

    private Log getLogger() {
      if (this.log == null) {
        this.log = LogFactory.getLog(this.getClass());
      }
      return this.log;
    }
  }

  private static final class ClosedExecutor extends BaseExecutor {

    public ClosedExecutor() {

View on GitHub (pinned to 008069adb1)

Solutions

  1. Change the factory to return the Configuration: sqlSessionFactory.getConfiguration()
  2. If it returned null, initialize the backing field before it can be called (lazy-build on first access)
  3. Keep the method signature exactly public static org.apache.ibatis.session.Configuration getConfiguration()

Example fix

// before
public static Configuration getConfiguration() {
  return App.sqlSessionFactory; // wrong type!
}
// after
public static Configuration getConfiguration() {
  return App.sqlSessionFactory.getConfiguration();
}
Defensive patterns

Strategy: type-guard

Validate before calling

Object o = MyConfigFactory.getConfiguration();
if (!(o instanceof Configuration)) throw new IllegalStateException("Factory must return Configuration, got: " + (o == null ? "null" : o.getClass()));

Type guard

static boolean factoryReturnsConfiguration() { try { return MyConfigFactory.class.getDeclaredMethod("getConfiguration").invoke(null) instanceof Configuration; } catch (ReflectiveOperationException e) { return false; } }

Prevention

When it happens

Trigger: LoadPair.getConfiguration() invokes the factory and configurationObject fails the instanceof Configuration check — factory returns SqlSessionFactory, a DataSource wrapper, some custom config object, or null.

Common situations: 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.

Related errors


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