Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot 2 disable Autocommit Hikari with multiple datasources

Tags:

I am attempting to turn off auto-commit on Hikari with multiple data sources, but I'm not having any luck. I'm using Spring Boot 2 (2.0.3.RELEASE). Here is my config:

application.properties

spring.datasource.primary.driver=com.mysql.cj.jdbc.Driver
spring.datasource.primary.url=jdbc:mysql://localhost:3306/spark?autoReconnect=true
spring.datasource.primary.username=xxxx
spring.datasource.primary.password=xxxx
spring.datasource.primary.max-active=100
spring.datasource.primary.max-idle=5
spring.datasource.primary.min-idle=1
spring.datasource.primary.test-while-idle=true
spring.datasource.primary.test-on-borrow=true
spring.datasource.primary.validation-query=SELECT 1
spring.datasource.primary.time-between-eviction-runs-millis=5000
spring.datasource.primary.min-evictable-idle-time-millis=60000


spring.datasource.ucm.driver=com.mysql.cj.jdbc.Driver
spring.datasource.ucm.url=jdbc:mysql://localhost:3306/usercentral?autoReconnect=true
spring.datasource.ucm.username=xxx
spring.datasource.ucm.password=xxx
spring.datasource.ucm.max-active=100
spring.datasource.ucm.test-while-idle=true
spring.datasource.ucm.test-on-borrow=true
spring.datasource.ucm.validation-query=SELECT 1
spring.datasource.ucm.time-between-eviction-runs-millis=5000
spring.datasource.ucm.min-evictable-idle-time-millis=60000
spring.datasource.ucm.hikari.auto-commit=false  # <- Not working

Here's my configuration class where the data sources are setup

@Primary
@Bean
@ConfigurationProperties("spring.datasource.primary")
public DataSourceProperties primaryDataSourceProperties() {
    return new DataSourceProperties();
}

@Bean
@Primary
@ConfigurationProperties("spring.datasource.primary")
public DataSource primaryDataSource() {
    return primaryDataSourceProperties().initializeDataSourceBuilder().build();
}

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

@Bean(name="ucmDataSource")
@ConfigurationProperties("spring.datasource.ucm")
public DataSource ucmDataSource() {
    return ucmDataSourceProperties().initializeDataSourceBuilder().build();
}

Here is what is being output when the pool is created:

-2018-08-23 15:48:22.845 -DEBUG 21455 --- [nio-8081-exec-1] com.zaxxer.hikari.HikariConfig           : 1151 : allowPoolSuspension.............false
-2018-08-23 15:48:22.846 -DEBUG 21455 --- [nio-8081-exec-1] com.zaxxer.hikari.HikariConfig           : 1151 : autoCommit......................true
-2018-08-23 15:48:22.846 -DEBUG 21455 --- [nio-8081-exec-1] com.zaxxer.hikari.HikariConfig           : 1151 : catalog.........................none
-2018-08-23 15:48:22.846 -DEBUG 21455 --- [nio-8081-exec-1] com.zaxxer.hikari.HikariConfig           : 1151 : connectionInitSql...............none

How do I turn off auto-commit on this connection pool?

like image 653
csyperski Avatar asked Aug 23 '18 15:08

csyperski


2 Answers

I am using Spring Boot 2.0.4.RELEASE, and faced the same problem. Spent hours with related posts showing non-working properties...

Removing .hikari worked for me.

spring.datasource.ucm.autocommit=false
like image 70
Water Avatar answered Sep 28 '22 17:09

Water


I know that this is very late but this issue exploded my brain for ~2 days and this post is at the top of google search so I will post here the solution for others.

It's actually quite simple, just that I missed it in the docs here https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources

long story short make sure that you add .type(HikariDataSource.class) before you build your class.

Also if you use jhipster you should also point your config to the hikari part like this @ConfigurationProperties("spring.datasource.other.hikari")

like image 25
Adrian Badarau Avatar answered Sep 28 '22 17:09

Adrian Badarau