Home > Enterprise >  Spring Boot only initializes one data source but not the other
Spring Boot only initializes one data source but not the other

Time:10-16

I have followed multiple articles on creating multiple data sources and tried the same in my code. But strangely spring boot in only initializing one data source and not the other. When I remove any one of them from the code, the other is created without any issues. Any idea.

Bean Configuration -

@Configuration
public class DBConfiguration {

    @Bean("dataSource")
    @ConfigurationProperties(prefix = "p.datasource")
    public DataSource pDataSource() {
        System.out.println("**************************> dataSource");
        return DataSourceBuilder.create().build();
    }

    @Bean("storeFrontDataSource")
    @ConfigurationProperties(prefix = "c.datasource")
    public DataSource cDataSource() {
        System.out.println("**************************> storeFrontDataSource");
        return DataSourceBuilder.create().build();
    }
}

YML File (removed some data for confidentiality) -

p:
  datasource:
    url: {some-url}
    driver-class-name: com.ibm.db2.jcc.DB2Driver
    username: abc
    password: xyz
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      schema:        
      connection-init-sql: select 1 from dual
      minimum-idle: 25
      maximum-pool-size: 100 
      idle-timeout: 10_000
      max-lifetime: 20_000
      connection-timeout: 1_000
      validation-timeout: 500

c:
  datasource:
    url: {some-url}
    driver-class-name: com.ibm.db2.jcc.DB2Driver
    username: abc
    password: xyz
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      schema:        
      pool-name: uspo-cis-sf-pool
      connection-init-sql: select 1 from dual
      minimum-idle: 1
      maximum-pool-size: 5 
      idle-timeout: 10_000
      max-lifetime: 50_000
      connection-timeout: 2_000 
      validation-timeout: 500

Error thrown -

Only the "datasource" bean is found and not the "storeFrontDataSource" bean. Even on the console only one of System.out.println() is called (i.e. for "datasource") while other is not printed. SpringBoot is not even initializing the other bean definition.

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zaxxer.hikari.HikariDataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=storeFrontDataSource)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1790) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1346) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-5.3.3.jar:5.3.3]
    ... 38 common frames omitted

Note - Even defining @Primary doesn't helps. Both System.out.println() should be printed to console regardless.

CodePudding user response:

As per the advise @tgdavies, I found some of the classes from the dependent libraries were injecting 'HikariDataSource' instead of 'DataSource'. After fixing them the issue got resolved.

CodePudding user response:

You need to create customer configuration class for create multiple datosource and you define primary datasource.

Check this example from baeldung

  • Related