I just upgraded my app from Spring Boot 1.5.9 to 2.1.3. We have 2 databases, one non sharded and the other one is sharded. When trying to start the application, i get the following error:

┌─────┐
|  shardedDataSource defined in class path resource [com/config/ShardedDataSourceConfig.class]
↑     ↓
|  globalNamedParameterJdbcTemplate defined in class path resource [com/config/GlobalDataSourceConfig.class]
↑     ↓
|  globalJdbcTemplate defined in class path resource [com/config/GlobalDataSourceConfig.class]
↑     ↓
|  global defined in class path resource [com/config/GlobalDataSourceConfig.class]
↑     ↓
|  org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker
└─────┘

In GlobalDataSourceConfig i have the following Beans

@Bean(name = "global")
    public DataSource globalDataSource() {
        return new HikariDataSource(this);
    }

    @Bean(name = "globalJdbcTemplate")
    public JdbcTemplate globalJdbcTemplate() {
        return new JdbcTemplate(globalDataSource());
    }

    @Bean(name="globalNamedParameterJdbcTemplate")
    public NamedParameterJdbcTemplate globalNamedParameterJdbcTemplate() {
        return new NamedParameterJdbcTemplate(globalJdbcTemplate());
    }

And for the shardedDataSource in ShardedDataSourceConfig class i have

  @Primary @Bean @Profile("!test")
    public DataSource shardedDataSource(@Qualifier("globalNamedParameterJdbcTemplate") NamedParameterJdbcTemplate jdbcTemplate) {

Anyone can give me an idea why this happens? Before the upgrade this was not happening. I found a similar issue but the solution is not working for SpringBoot 2

https://github.com/spring-projects/spring-boot/issues/9394

Comment From: wilkinsona

It's very hard to say with this might be happening based only on 4 bean definitions. Please try upgrading to the latest Spring Boot 2.1.x or 2.2.x release. If the problem remains, please provide a small sample (a complete, minimal project in an attached zip or separate GitHub repo) that reproduces the problem and we can take a look.

Comment From: AleksandarTokarev

The repository provided by zach in https://github.com/spring-projects/spring-boot/issues/9394
gives a good explanation of the problem in the Readme https://github.com/zachmarshall/spring-datasource-init-bug

Atm we are kinda limited to using Spring Boot 2.1.3 because of some other dependencies that we have.

Comment From: wilkinsona

The problem in #9394 has a workaround but you said above that "the solution is not working" so, presumably, your problem isn't the same. Without knowing more about wha is different in your case we aren't going to be able to help you. If you would like us to spend some more time looking at this, please take the time to provide the information that we need to be able to do so. In this case that's a small sample project that reproduces your specific problem.

Comment From: AleksandarTokarev

Well if u look at the issue in #9394 , @rvit34 has stated that the solution is not working for him as well in Spring Boot 2

Comment From: wilkinsona

That's not entirely surprising as @rvit34 has a different (and more complicated) setup.

As I said above, if you would like us to spend some more time looking at this, please take the time to provide the information that we need to be able to do so. If you aren't in a position to do so then I'm afraid you'll have to look elsewhere for some help.

Comment From: AleksandarTokarev

Please have a look at this Branch, which fails on Spring Boot 2 https://github.com/zachmarshall/spring-datasource-init-bug/tree/spring-boot-2 He has this problem explained in this branch. The test in the class TestPassesWithInitializerDisabled is an example.

Comment From: wilkinsona

I see you've also posted this on Stack Overflow. In the interests of avoiding duplicated effort, I'm going to close this issue. If you update your question on Stack Overflow with a minimal, complete, and verifiable example I or someone else in the Spring Boot community may be able to help you. Please note that the example should be for your specific problem rather than someone else's. DataSource initialization is more complicated then we would like and a fix or workaround for someone else's problem may not help you with yours.