Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hikari CP properties are not working with Multiple datasource configuration in Spring 1.5.18

We are using multiple datasource configuration in our spring boot app. both dataasources belongs to mysql only.

Configured multiple data source using:

https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7

pom.xml changes:

<!-- exclude tomcat jdbc connection pool, use HikariCP -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- exclude tomcat-jdbc, Spring Boot will use HikariCP automatically -->
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
</dependency>

.properties:

spring.db1.datasource.jdbcUrl=jdbc:mysql://localhost:3306/db1?zeroDateTimeBehavior=convertToNull
spring.db1.datasource.driverClassName=com.mysql.jdbc.Driver
spring.db1.datasource.username=root
spring.db1.datasource.password=
spring.db1.datasource.hikari.maximum-pool-size=5
spring.db1.datasource.hikari.minimum-idle=1

spring.db2.datasource.jdbcUrl=jdbc:mysql://localhost:3306/db2?zeroDateTimeBehavior=convertToNull
spring.db2.datasource.driverClassName=com.mysql.jdbc.Driver
spring.db2.datasource.username=root
spring.db2.datasource.password=
spring.db2.datasource.hikari.maximum-pool-size=5
spring.db2.datasource.hikari.minimum-idle=1

Datasource Bean Config:

@Bean
@ConfigurationProperties(prefix = "spring.db1.datasource")
public DataSource db1DataSource() {
    return DataSourceBuilder.create().build();
}


@Bean
@ConfigurationProperties(prefix = "spring.db2.datasource")
public DataSource db2Source() {
    return DataSourceBuilder.create().build();
}   

When I start the application, its creating 10 connections of db1 (@Primary datasource) by default but its not considering maximum-pool-size and minimum-idle properties. Also its not creating connections for db2.

Is there any configuration missed ? please help

like image 899
Krish Avatar asked Oct 21 '25 17:10

Krish


1 Answers

Make sure you have @Primary

@Primary
@Bean
@ConfigurationProperties(prefix = "spring.db1.datasource")
public DataSource db1DataSource() {
    return DataSourceBuilder.create().build();
}


@Bean
@ConfigurationProperties(prefix = "spring.db2.datasource")
public DataSource db2Source() {
    return DataSourceBuilder.create().build();
} 

According to https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html you can define the properties as following

spring.db1.datasource.jdbcUrl=jdbc:mysql://localhost:3306/db1?zeroDateTimeBehavior=convertToNull
spring.db1.datasource.username=root
spring.db1.datasource.password=
spring.db1.datasource.maximum-pool-size=5
spring.db1.datasource.minimumIdle=1

spring.db2.datasource.jdbcUrl=jdbc:mysql://localhost:3306/db2?zeroDateTimeBehavior=convertToNull
spring.db2.datasource.username=root
spring.db2.datasource.password=
spring.db2.datasource.maximum-pool-size=5
spring.db2.datasource.minimumIdle=1

Also using DataSourceBuilder can auto-detect driver base on the connection string. You can define logging.level.com.zaxxer.hikari.HikariConfig=TRACE to see that your connection properties work.

like image 75
Omar Avatar answered Oct 24 '25 21:10

Omar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!