{"record":{"id":"5849d41d0e41f619","repo":"mybatis/mybatis-3","slug":"datasource-cannot-be-null","errorCode":null,"errorMessage":"dataSource cannot be null","messagePattern":"dataSource cannot be null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/mapping/VendorDatabaseIdProvider.java","lineNumber":42,"sourceCode":"import org.apache.ibatis.builder.BuilderException;\n\n/**\n * Vendor DatabaseId provider.\n * <p>\n * It returns database product name as a databaseId. If the user provides a properties it uses it to translate database\n * product name key=\"Microsoft SQL Server\", value=\"ms\" will return \"ms\". It can return null, if no database product name\n * or a properties was specified and no translation was found.\n *\n * @author Eduardo Macarron\n */\npublic class VendorDatabaseIdProvider implements DatabaseIdProvider {\n\n  private Properties properties;\n\n  @Override\n  public String getDatabaseId(DataSource dataSource) {\n    if (dataSource == null) {\n      throw new NullPointerException(\"dataSource cannot be null\");\n    }\n    try {\n      return getDatabaseName(dataSource);\n    } catch (SQLException e) {\n      throw new BuilderException(\"Error occurred when getting DB product name.\", e);\n    }\n  }\n\n  @Override\n  public void setProperties(Properties p) {\n    this.properties = p;\n  }\n\n  private String getDatabaseName(DataSource dataSource) throws SQLException {\n    String productName = getDatabaseProductName(dataSource);\n    if (properties == null || properties.isEmpty()) {\n      return productName;\n    }","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/mapping/VendorDatabaseIdProvider.java#L24-L60","documentation":"VendorDatabaseIdProvider.getDatabaseId() throws NullPointerException when the passed DataSource is null. The provider must open a connection to read DatabaseMetaData.getDatabaseProductName(), so a null DataSource cannot be handled.","triggerScenarios":"Calling new VendorDatabaseIdProvider().getDatabaseId(null), or building a Configuration/XMLConfigBuilder where a databaseIdProvider is configured but the environment/DataSource is missing so MyBatis passes null.","commonSituations":"Configuration built programmatically and dataSource set after the databaseIdProvider is applied; XML config with <databaseIdProvider> but a missing or unnamed <environment> that leaves dataSource null during parsing.","solutions":["Ensure the Environment with a non-null DataSource is set on the Configuration before the databaseIdProvider resolves (defaultEnvironmentRef points at a real environment).","If calling the provider directly, pass the actual DataSource instance.","Null-check the DataSource before invoking getDatabaseId if it may legitimately be absent."],"exampleFix":"// before\nString dbId = provider.getDatabaseId(null);\n// after\nDataSource ds = configuration.getEnvironment().getDataSource();\nString dbId = provider.getDatabaseId(ds);","handlingStrategy":"type-guard","validationCode":"if (dataSource == null) throw new IllegalStateException(\"Environment/DataSource not configured before databaseId resolution\");","typeGuard":"DataSource ds = configuration.getEnvironment() != null ? configuration.getEnvironment().getDataSource() : null;\nif (ds == null) { /* skip databaseId resolution */ }","tryCatchPattern":null,"preventionTips":["Wire the Environment (with DataSource) into Configuration before applying any DatabaseIdProvider.","In tests, guard getDatabaseId calls behind a null check on the DataSource."],"tags":["mybatis","datasource","configuration","null-check"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}