{"record":{"id":"3bc7dd9c315ede5a","repo":"mybatis/mybatis-3","slug":"parameter-datasource-must-not-be-null","errorCode":null,"errorMessage":"Parameter 'dataSource' must not be null","messagePattern":"Parameter 'dataSource' must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/mapping/Environment.java","lineNumber":39,"sourceCode":"\n/**\n * @author Clinton Begin\n */\npublic final class Environment {\n  private final String id;\n  private final TransactionFactory transactionFactory;\n  private final DataSource dataSource;\n\n  public Environment(String id, TransactionFactory transactionFactory, DataSource dataSource) {\n    if (id == null) {\n      throw new IllegalArgumentException(\"Parameter 'id' must not be null\");\n    }\n    if (transactionFactory == null) {\n      throw new IllegalArgumentException(\"Parameter 'transactionFactory' must not be null\");\n    }\n    this.id = id;\n    if (dataSource == null) {\n      throw new IllegalArgumentException(\"Parameter 'dataSource' must not be null\");\n    }\n    this.transactionFactory = transactionFactory;\n    this.dataSource = dataSource;\n  }\n\n  public static class Builder {\n    private final String id;\n    private TransactionFactory transactionFactory;\n    private DataSource dataSource;\n\n    public Builder(String id) {\n      this.id = id;\n    }\n\n    public Builder transactionFactory(TransactionFactory transactionFactory) {\n      this.transactionFactory = transactionFactory;\n      return this;\n    }","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/mapping/Environment.java#L21-L57","documentation":"Environment's constructor throws IllegalArgumentException when dataSource is null (checked last, after id and transactionFactory). Without a DataSource the environment cannot open connections, so MyBatis rejects it immediately.","triggerScenarios":"new Environment(\"dev\", txFactory, null) or Environment.Builder.build() without calling dataSource(...) — commonly the DataSource failed to construct (DB URL typo causing a wrapped exception swallowed upstream) or the builder step was skipped.","commonSituations":"Programmatic configuration where the pooled DataSource creation code errored and the field stayed null; forgetting builder.dataSource(ds) when using Environment.Builder; unit tests building Configuration without a real DataSource.","solutions":["Provide a DataSource: e.g. UNPOOLED via org.apache.ibatis.datasource.unpooled.UnpooledDataSource or a pooled/HikariCP DataSource.","If using Environment.Builder, ensure .dataSource(ds) is called before .build().","Check upstream creation of the DataSource pool for swallowed exceptions (log them instead) so it never reaches Environment as null."],"exampleFix":"// before\nEnvironment env = new Environment.Builder(\"dev\")\n    .transactionFactory(new JdbcTransactionFactory())\n    .build(); // dataSource never set -> null\n\n// after\nEnvironment env = new Environment.Builder(\"dev\")\n    .transactionFactory(new JdbcTransactionFactory())\n    .dataSource(new UnpooledDataSource(driver, url, user, pass))\n    .build();","handlingStrategy":"validation","validationCode":"DataSource ds = Objects.requireNonNull(dataSource, \"DataSource creation failed\");\nnew Environment.Builder(\"dev\").transactionFactory(txf).dataSource(ds).build();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate DataSource pool creation at startup with a real getConnection() ping.","When using Environment.Builder, chain .dataSource(...) before .build() and review the chain in code review.","Do not swallow exceptions from pool construction; let startup fail with the underlying cause."],"tags":["mybatis","configuration","datasource","null-check"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}