Search code examples
spring-boothikaricp

Configure HikariCP properties with multiple datasources


I have defined two datasources for my spring application:

spring.datasource.audit.url=...
spring.datasource.audit.username=...
spring.datasource.audit.password=...
spring.datasource.audit.driver-class-name=oracle.jdbc.OracleDriver

spring.datasource.fic.url=...
spring.datasource.fic.username=...
spring.datasource.fic.password=...
spring.datasource.fic.driver-class-name=oracle.jdbc.OracleDriver

For the fic datasource, I need a certain SQL to be executed just after the connection is created. I found the connectionInitSql property, but I cannot get Hikari to load the value.

For creating the fic datasource, I have the following configuration class:

@Configuration
public class DataSourceConfiguration {

    @Bean
    @ConfigurationProperties("spring.datasource.fic")
    public DataSourceProperties dataSourcePropertiesFic() {
        return new DataSourceProperties();
    } 

    @Bean
    public DataSource dataSourceFic() {
        return dataSourcePropertiesFic()
          .initializeDataSourceBuilder()
          .build();
    }
    
    @Bean
    public JdbcTemplate jdbcTemplateFic(@Qualifier("dataSourceFic") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

I also have an equivalent configuration class for the audit datasource.

I have added the following lines to my application.properties. Obviously it would fail if Hikari attempted to execute any of the sentences; also I have setup Hikari's log level to debug and it shows the connectionInitSql property to be null for both connection pools:

spring.datasource.fic.connectionInitSql=HOLA
spring.datasource.fic.hikari.connectionInitSql=HOLA
spring.datasource.hikari.connectionInitSql=HOLA

How could I pass that connectionInitSql to Hikari, only for the uip datasource?

I am using SpringBoot 3.0.4 with HikariCP 5.0.1


Solution

  • spring.datasource.fic.hikari.connectionInitSql should work. However you are missing an @ConfigurationProperties("spring.datasource.fic.hikari") on your dataSourceFix() bean method!

    This is also explained/shown in the Spring Boot documentation. The first is to create the generic properties, the second is for binding the datasource specific properties to the implementation.