baomidou/mybatis-plus · critical · IOException
Failed getting a databaseId
Error message
Failed getting a databaseId
What it means
Thrown by MybatisSqlSessionFactoryBean.buildSqlSessionFactory during Spring SqlSessionFactory bootstrap: when a databaseIdProvider is configured, it calls databaseIdProvider.getDatabaseId(dataSource), and a SQLException from that probe is wrapped in IOException('Failed getting a databaseId') so startup fails fast. The SQLException cause identifies the real connectivity or provider problem.
Source
Thrown at mybatis-plus-spring/src/main/java/com/baomidou/mybatisplus/spring/MybatisSqlSessionFactoryBean.java:650
});
}
targetConfiguration.setDefaultEnumTypeHandler(defaultEnumTypeHandler);
if (!isEmpty(this.scriptingLanguageDrivers)) {
Stream.of(this.scriptingLanguageDrivers).forEach(languageDriver -> {
targetConfiguration.getLanguageRegistry().register(languageDriver);
LOGGER.debug(() -> "Registered scripting language driver: '" + languageDriver + "'");
});
}
Optional.ofNullable(this.defaultScriptingLanguageDriver)
.ifPresent(targetConfiguration::setDefaultScriptingLanguage);
if (this.databaseIdProvider != null) {// fix #64 set databaseId before parse mapper xmls
try {
targetConfiguration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
} catch (SQLException e) {
throw new IOException("Failed getting a databaseId", e);
}
}
Optional.ofNullable(this.cache).ifPresent(targetConfiguration::addCache);
if (xmlConfigBuilder != null) {
try {
xmlConfigBuilder.parse();
LOGGER.debug(() -> "Parsed configuration file: '" + this.configLocation + "'");
} catch (Exception ex) {
throw new IOException("Failed to parse config resource: " + this.configLocation, ex);
} finally {
ErrorContext.instance().reset();
}
}
targetConfiguration.setEnvironment(new Environment(this.environment,
this.transactionFactory == null ? new SpringManagedTransactionFactory() : this.transactionFactory,View on GitHub (pinned to bf67d90747)
Solutions
- Check the wrapped SQLException cause for the concrete JDBC failure and fix connectivity/credentials/driver
- Verify the DataSource bean the factory uses points at a reachable database (test with a plain Connection in a breakpoint or health check)
- In orchestrated environments, ensure the database is a startup dependency (depends-on, container healthchecks) or remove databaseIdProvider if databaseId is not actually used
- If mapper XMLs use databaseId, confirm the provider is configured with correct properties (e.g. VendorDatabaseIdProvider 'Oracle'->'oracle' mappings)
Example fix
// before: factory initializes while DB is down
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource ds) throws Exception {
MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
factory.setDataSource(ds);
factory.setDatabaseIdProvider(new VendorDatabaseIdProvider());
return factory.getObject();
}
// after: only set provider when it is actually needed, after DB is reachable
factory.setDatabaseIdProvider(databaseIdUsed ? new VendorDatabaseIdProvider() : null); Defensive patterns
Strategy: try-catch
Validate before calling
// Fail fast with a clear message before building the factory
try (Connection ignored = dataSource.getConnection()) {
// DB reachable
} catch (SQLException e) {
throw new IllegalStateException("Database unreachable; cannot resolve databaseId", e);
} Try / catch
try {
sqlSessionFactory(bean).getObject();
} catch (IOException e) {
if ("Failed getting a databaseId".equals(e.getMessage())) {
// inspect e.getCause() SQLException; verify DB URL/credentials/startup order
throw new IllegalStateException("databaseIdProvider could not reach the database", e.getCause());
}
throw e;
} Prevention
- Make the database a hard startup dependency (container healthchecks, depends-on)
- Only configure databaseIdProvider when mapper XMLs actually use databaseId
- Keep DB credentials in managed config and validate them at deploy time
When it happens
Trigger: Setting the databaseIdProvider property on MybatisSqlSessionFactoryBean (mybatis-plus-spring) while the DataSource cannot serve the provider's metadata query — DB down, wrong URL/credentials, driver missing, or a custom VendorDatabaseIdProvider whose getDatabaseName query fails.
Common situations: Application startup order issues where the bean initializes before the DB is reachable (containers/K8s); credentials rotated and not updated; using databaseIdProvider for multi-dialect mapper support and the connection URL is misconfigured.
Related errors
- Failed to parse config resource:
- Failed to parse mapping resource: '
- 获取自定义字段错误:
- 读取[%s]字段出错!
- connection cannot be null
AI-assisted analysis of baomidou/mybatis-plus@bf67d90747 (2026-08-14).
Data as JSON: /api/errors/8b35b0aea8798ca0.
Report an issue: GitHub.