Skip to content

[Bug] Boot 3/4 app with actuator fails to start when the primary datasource is missing #816

Closed

Description

@linghengqian

使用的 starter 模块 / Starter artifact

dynamic-datasource-spring-boot3-starter (Spring Boot 3.x.x)

dynamic-datasource-spring-boot4-starter fails the same way. The Boot 2 starter is not affected.

dynamic-datasource 版本 (required)

Reproduced on dev @ 65b8249, which builds as 4.7.0. The code involved is the same in 4.6.0, and #689 shows the same stack trace on 4.3.1.

Spring Boot 版本 (required)

3.5.16 and 4.0.8. 2.7.18 is not affected.

JDK 版本 (required)

openjdk version "17.0.20.1" 2026-08-18
OpenJDK Runtime Environment Temurin-17.0.20.1+1 (build 17.0.20.1+1)
OpenJDK 64-Bit Server VM Temurin-17.0.20.1+1 (build 17.0.20.1+1, mixed mode, sharing)

连接池与版本 (optional)

Not pool-specific (HikariCP in the reproducer).

问题描述 / Describe the bug

With spring-boot-starter-actuator on the classpath, the application fails to start if DynamicRoutingDataSource has no primary datasource at startup. This is easy to hit:

  • primary is not set, so it defaults to master, but none of the datasources is called master.
  • No datasource exists at startup, because they are all added later with addDataSource(), or because they come from a config center that was not reachable. [错误报告]: 升级到3.5.9后,启动报异常 #689 is this case with Nacos. It was closed as a Nacos problem, but the startup failure itself comes from the code below.

On Boot 2.7.18 the same application starts and logs Please add your primary datasource or check your configuration. On Boot 3.5.16 and 4.0.8 it fails while creating the dbHealthContributor bean.

Since 3.2.10 and 3.3.4 (spring-projects/spring-boot#42313), Boot's DataSourceHealthContributorAutoConfiguration#isRoutingDataSource calls dataSource.isWrapperFor(AbstractRoutingDataSource.class) and only catches SQLException. Our AbstractRoutingDataSource#isWrapperFor is

return (iface.isInstance(this) || determineDataSource().isWrapperFor(iface));

During startup nothing has been pushed to DynamicDataSourceContextHolder, so determineDataSource() falls back to the primary datasource, and determinePrimaryDataSource() throws CannotFindDataSourceException. That is a RuntimeException. It gets past Boot's catch (SQLException ex) and aborts the context refresh. The Boot 2.7 version of that class only checks instanceof AbstractRoutingDataSource, so Boot 2 never gets here.

期望结果 / Expected behavior

The application starts, as it does on Boot 2, and the existing warning tells the user that the primary datasource is missing.

One way to fix it: when determineDataSource() throws CannotFindDataSourceException inside isWrapperFor, rethrow it as an SQLException. That is how the JDBC Wrapper contract reports errors, and Boot then falls back to a plain DataSourceHealthIndicator for the routing datasource. I tried this on dev, and the reproducer below then starts on both 3.5.16 and 4.0.8.

实际结果 / Actual behavior

Startup fails with Error creating bean with name 'dbHealthContributor' ... dynamic-datasource can not find primary datasource. Full trace below. Setting management.health.db.enabled=false works around it.

相关配置 / Relevant configuration

spring:
  datasource:
    dynamic:
      # primary is not set, so it defaults to master
      datasource:
        db1:
          url: jdbc:h2:mem:db1
          username: sa
          driver-class-name: org.h2.Driver

复现步骤 / Minimal reproducer

On dev, add this test to dynamic-datasource-spring-boot3-starter/src/test/java. The starter's test classpath already has actuator, web and H2.

package com.baomidou.dynamic.datasource.repro;

import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = PrimaryNotFoundTest.App.class, properties = {
        "spring.datasource.dynamic.datasource.db1.url=jdbc:h2:mem:db1",
        "spring.datasource.dynamic.datasource.db1.username=sa",
        "spring.datasource.dynamic.datasource.db1.driver-class-name=org.h2.Driver"
})
class PrimaryNotFoundTest {

    @Test
    void contextLoads() {
    }

    @SpringBootConfiguration
    @EnableAutoConfiguration
    static class App {
    }
}
  1. ./gradlew :dynamic-datasource-spring-boot3-starter:test --tests '*PrimaryNotFoundTest' fails because the context cannot load (CannotFindDataSourceException). The same test fails in the Boot 4 starter.
  2. In dynamic-datasource-spring-boot-starter (Boot 2.7.18) the same test passes and only logs the warning.
  3. With the three properties removed, so that no datasource is configured at all, Boot 3 and 4 still fail.
  4. With management.health.db.enabled=false added, the test passes on Boot 3 and 4.

异常栈 / Stack trace

This is the same configuration started through a plain SpringApplication.run(...) main class on Boot 3.5.16, so there are no test frames:

2026-10-17T20:00:53.389+08:00  WARN 378256 --- [           main] c.b.d.d.DynamicRoutingDataSource         : dynamic-datasource initial loaded [1] datasource,Please add your primary datasource or check your configuration
2026-10-17T20:00:53.516+08:00  WARN 378256 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthContributorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthContributorRegistry' parameter 2: Error creating bean with name 'dbHealthContributor' defined in class path resource [org/springframework/boot/actuate/autoconfigure/jdbc/DataSourceHealthContributorAutoConfiguration.class]: Failed to instantiate [org.springframework.boot.actuate.health.HealthContributor]: Factory method 'dbHealthContributor' threw exception with message: dynamic-datasource can not find primary datasource
2026-10-17T20:00:53.517+08:00  INFO 378256 --- [           main] c.b.d.d.DynamicRoutingDataSource         : dynamic-datasource start closing ....
2026-10-17T20:00:53.520+08:00  INFO 378256 --- [           main] com.zaxxer.hikari.HikariDataSource       : db1 - Shutdown initiated...
2026-10-17T20:00:53.526+08:00  INFO 378256 --- [           main] com.zaxxer.hikari.HikariDataSource       : db1 - Shutdown completed.
2026-10-17T20:00:53.527+08:00  INFO 378256 --- [           main] c.b.d.d.d.DefaultDataSourceDestroyer     : dynamic-datasource close the datasource named [db1] success,
2026-10-17T20:00:53.527+08:00  INFO 378256 --- [           main] c.b.d.d.DynamicRoutingDataSource         : dynamic-datasource all closed success,bye
2026-10-17T20:00:53.538+08:00  INFO 378256 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2026-10-17T20:00:53.577+08:00  INFO 378256 --- [           main] .s.b.a.l.ConditionEvaluationReportLogger :

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2026-10-17T20:00:53.607+08:00 ERROR 378256 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthContributorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthContributorRegistry' parameter 2: Error creating bean with name 'dbHealthContributor' defined in class path resource [org/springframework/boot/actuate/autoconfigure/jdbc/DataSourceHealthContributorAutoConfiguration.class]: Failed to instantiate [org.springframework.boot.actuate.health.HealthContributor]: Factory method 'dbHealthContributor' threw exception with message: dynamic-datasource can not find primary datasource
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:804) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:546) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1373) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1203) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:569) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:529) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:339) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:373) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:337) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.instantiateSingleton(DefaultListableBeanFactory.java:1235) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingleton(DefaultListableBeanFactory.java:1201) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:1130) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:991) ~[spring-context-6.2.19.jar:6.2.19]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:628) ~[spring-context-6.2.19.jar:6.2.19]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.5.16.jar:3.5.16]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) ~[spring-boot-3.5.16.jar:3.5.16]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) ~[spring-boot-3.5.16.jar:3.5.16]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:318) ~[spring-boot-3.5.16.jar:3.5.16]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1361) ~[spring-boot-3.5.16.jar:3.5.16]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1350) ~[spring-boot-3.5.16.jar:3.5.16]
	at com.baomidou.dynamic.datasource.repro.ReproMain.main(ReproMain.java:8) ~[test/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbHealthContributor' defined in class path resource [org/springframework/boot/actuate/autoconfigure/jdbc/DataSourceHealthContributorAutoConfiguration.class]: Failed to instantiate [org.springframework.boot.actuate.health.HealthContributor]: Factory method 'dbHealthContributor' threw exception with message: dynamic-datasource can not find primary datasource
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:657) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:645) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1373) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1203) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:569) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:529) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:339) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:373) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:337) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:2029) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1992) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeanMap(DefaultListableBeanFactory.java:1914) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeans(DefaultListableBeanFactory.java:1853) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1726) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1660) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:913) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-6.2.19.jar:6.2.19]
	... 21 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthContributor]: Factory method 'dbHealthContributor' threw exception with message: dynamic-datasource can not find primary datasource
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:200) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiateWithFactoryMethod(SimpleInstantiationStrategy.java:89) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:169) ~[spring-beans-6.2.19.jar:6.2.19]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-6.2.19.jar:6.2.19]
	... 39 common frames omitted
Caused by: com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException: dynamic-datasource can not find primary datasource
	at com.baomidou.dynamic.datasource.DynamicRoutingDataSource.determinePrimaryDataSource(DynamicRoutingDataSource.java:125) ~[dynamic-datasource-spring-4.7.0.jar:na]
	at com.baomidou.dynamic.datasource.DynamicRoutingDataSource.getDataSource(DynamicRoutingDataSource.java:154) ~[dynamic-datasource-spring-4.7.0.jar:na]
	at com.baomidou.dynamic.datasource.DynamicRoutingDataSource.determineDataSource(DynamicRoutingDataSource.java:111) ~[dynamic-datasource-spring-4.7.0.jar:na]
	at com.baomidou.dynamic.datasource.ds.AbstractRoutingDataSource.isWrapperFor(AbstractRoutingDataSource.java:94) ~[dynamic-datasource-spring-4.7.0.jar:na]
	at org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration.isRoutingDataSource(DataSourceHealthContributorAutoConfiguration.java:128) ~[spring-boot-actuator-autoconfigure-3.5.16.jar:3.5.16]
	at org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration.createContributor(DataSourceHealthContributorAutoConfiguration.java:112) ~[spring-boot-actuator-autoconfigure-3.5.16.jar:3.5.16]
	at org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration.createContributor(DataSourceHealthContributorAutoConfiguration.java:106) ~[spring-boot-actuator-autoconfigure-3.5.16.jar:3.5.16]
	at org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration.dbHealthContributor(DataSourceHealthContributorAutoConfiguration.java:100) ~[spring-boot-actuator-autoconfigure-3.5.16.jar:3.5.16]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:569) ~[na:na]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:172) ~[spring-beans-6.2.19.jar:6.2.19]
	... 42 common frames omitted

On Boot 4.0.8 the root cause is the same, only the Boot class has moved:

Caused by: com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException: dynamic-datasource can not find primary datasource
	at com.baomidou.dynamic.datasource.DynamicRoutingDataSource.determinePrimaryDataSource(DynamicRoutingDataSource.java:125) ~[dynamic-datasource-spring-4.7.0.jar:na]
	at com.baomidou.dynamic.datasource.DynamicRoutingDataSource.getDataSource(DynamicRoutingDataSource.java:154) ~[dynamic-datasource-spring-4.7.0.jar:na]
	at com.baomidou.dynamic.datasource.DynamicRoutingDataSource.determineDataSource(DynamicRoutingDataSource.java:111) ~[dynamic-datasource-spring-4.7.0.jar:na]
	at com.baomidou.dynamic.datasource.ds.AbstractRoutingDataSource.isWrapperFor(AbstractRoutingDataSource.java:94) ~[dynamic-datasource-spring-4.7.0.jar:na]
	at org.springframework.boot.jdbc.autoconfigure.health.DataSourceHealthContributorAutoConfiguration.isRoutingDataSource(DataSourceHealthContributorAutoConfiguration.java:130) ~[spring-boot-jdbc-4.0.8.jar:4.0.8]
	at org.springframework.boot.jdbc.autoconfigure.health.DataSourceHealthContributorAutoConfiguration.createContributor(DataSourceHealthContributorAutoConfiguration.java:114) ~[spring-boot-jdbc-4.0.8.jar:4.0.8]
	at org.springframework.boot.jdbc.autoconfigure.health.DataSourceHealthContributorAutoConfiguration.createContributor(DataSourceHealthContributorAutoConfiguration.java:108) ~[spring-boot-jdbc-4.0.8.jar:4.0.8]
	at org.springframework.boot.jdbc.autoconfigure.health.DataSourceHealthContributorAutoConfiguration.dbHealthContributor(DataSourceHealthContributorAutoConfiguration.java:102) ~[spring-boot-jdbc-4.0.8.jar:4.0.8]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:569) ~[na:na]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:155) ~[spring-beans-7.0.9.jar:7.0.9]
	... 90 common frames omitted

提交前确认 / Preflight

  • 我已搜索过 open/closed issues 与 Discussions,没有重复
  • 我已确认 JDK / Spring Boot 与 starter 模块的版本是匹配的(见 README 版本选择表)
  • 我已对配置内容脱敏(不含真实密码/密钥)

Activity

  1. linghengqian commented on Oct 17, 2026

    @linghengqian
    ContributorAuthor

    I am working on the current issue.

  2. linghengqian commented on Oct 17, 2026

    @linghengqian
    ContributorAuthor
  3. linghengqian commented on Oct 17, 2026

    @linghengqian
    ContributorAuthor
    • Out of curiosity, I would expect a GitHub bot to automatically close the relevant issue once a PR is merged; otherwise, the Fixes statement serves no purpose.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions