{"record":{"id":"8b35b0aea8798ca0","repo":"baomidou/mybatis-plus","slug":"failed-getting-a-databaseid","errorCode":null,"errorMessage":"Failed getting a databaseId","messagePattern":"Failed getting a databaseId","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"critical","filePath":"mybatis-plus-spring/src/main/java/com/baomidou/mybatisplus/spring/MybatisSqlSessionFactoryBean.java","lineNumber":650,"sourceCode":"            });\n        }\n\n        targetConfiguration.setDefaultEnumTypeHandler(defaultEnumTypeHandler);\n\n        if (!isEmpty(this.scriptingLanguageDrivers)) {\n            Stream.of(this.scriptingLanguageDrivers).forEach(languageDriver -> {\n                targetConfiguration.getLanguageRegistry().register(languageDriver);\n                LOGGER.debug(() -> \"Registered scripting language driver: '\" + languageDriver + \"'\");\n            });\n        }\n        Optional.ofNullable(this.defaultScriptingLanguageDriver)\n            .ifPresent(targetConfiguration::setDefaultScriptingLanguage);\n\n        if (this.databaseIdProvider != null) {// fix #64 set databaseId before parse mapper xmls\n            try {\n                targetConfiguration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));\n            } catch (SQLException e) {\n                throw new IOException(\"Failed getting a databaseId\", e);\n            }\n        }\n\n        Optional.ofNullable(this.cache).ifPresent(targetConfiguration::addCache);\n\n        if (xmlConfigBuilder != null) {\n            try {\n                xmlConfigBuilder.parse();\n                LOGGER.debug(() -> \"Parsed configuration file: '\" + this.configLocation + \"'\");\n            } catch (Exception ex) {\n                throw new IOException(\"Failed to parse config resource: \" + this.configLocation, ex);\n            } finally {\n                ErrorContext.instance().reset();\n            }\n        }\n\n        targetConfiguration.setEnvironment(new Environment(this.environment,\n            this.transactionFactory == null ? new SpringManagedTransactionFactory() : this.transactionFactory,","sourceCodeStart":632,"sourceCodeEnd":668,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-spring/src/main/java/com/baomidou/mybatisplus/spring/MybatisSqlSessionFactoryBean.java#L632-L668","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)"],"exampleFix":"// before: factory initializes while DB is down\n@Bean\npublic SqlSessionFactory sqlSessionFactory(DataSource ds) throws Exception {\n  MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();\n  factory.setDataSource(ds);\n  factory.setDatabaseIdProvider(new VendorDatabaseIdProvider());\n  return factory.getObject();\n}\n// after: only set provider when it is actually needed, after DB is reachable\nfactory.setDatabaseIdProvider(databaseIdUsed ? new VendorDatabaseIdProvider() : null);","handlingStrategy":"try-catch","validationCode":"// Fail fast with a clear message before building the factory\ntry (Connection ignored = dataSource.getConnection()) {\n    // DB reachable\n} catch (SQLException e) {\n    throw new IllegalStateException(\"Database unreachable; cannot resolve databaseId\", e);\n}","typeGuard":null,"tryCatchPattern":"try {\n    sqlSessionFactory(bean).getObject();\n} catch (IOException e) {\n    if (\"Failed getting a databaseId\".equals(e.getMessage())) {\n        // inspect e.getCause() SQLException; verify DB URL/credentials/startup order\n        throw new IllegalStateException(\"databaseIdProvider could not reach the database\", e.getCause());\n    }\n    throw e;\n}","preventionTips":["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"],"tags":["mybatis-plus","spring","startup","jdbc","database-id"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}