简体   繁体   中英

Spring boot app will not fail fast when custom transaction manager fails to connect

I am making a spring boot app with a custom transactionManager with its own datasource like so. Btw I'm using hikariCP as the datasource

@Configuration
@EnableTransactionManagement
@EnableJpaRespositories(
basePackages = "..."
entityManagerFactoryRef = "..."
transactionManagerRef = "..."
)
public class myDataSourceConfigration {
    @Bean
    @Primary
    @Configuration("myconfig.datasource")
    public DataSourceProperties myconfigDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @Configuration("myconfig.datasource.configurations")
    public DataSource myconfigDataSource(){
        return myconfigDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build;
    }

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean myconfigEntityManagerFactory(EntityManagerFactoryBuilder builder){
        return builder                
                 .dataSource(myconfigDataSource)
                 .packages(MyClass.class)
                 .build;
    }

    @Bean
    @Primary
    public PlatformTransactionManager myconfigTransactionManager(
             final @Qualifier ("myconfigEntityManagerFactory")LocalContainerEntityManagerFactoryBean myconfigEntityManagerFactory){
        return new JpaTransactionManager(myconfigEntityManagerFactory.getObject());
    }
}

In my application.properties, I have these configurations

myconfig.datasource.url:url
myconfig.datasource.username:username
myconfig.datasource.password:password
myconfig.datasource.driverClassName:driverClass

Now this all works when its fine but the issue is when it doesn't work. For example, if I put the wrong password, hikari throws a connection pool exception but the app keeps running. I want the app to fail, throw an exception and shutdown but I am having trouble figuring that out. I tried adding another configuration called initializationFailTimeout like so myConfig.datasource.initializationFailTimeout:5000 but same issue, I see an exception but the app keeps running.

Anyway on how to make spring boot fail fast when hikari throws a connection exception?

Thanks

I managed to fix it by initializing HikariConfig bean first and then pass it into HikariDataSource constructor:

@Bean("myDatasourceHikariConfig")
@ConfigurationProperties(prefix = "spring.datasource")
public HikariConfig hikariConfig() {
    return new HikariConfig();
}

@Bean("myDatasource")
public DataSource myDataSource(@Qualifier("myDatasourceHikariConfig") HikariConfig hikariConfig) {
    return new HikariDataSource(hikariConfig);
}

The corresponding part of application.properties :

spring.datasource.jdbc-url: jdbc-url
spring.datasource.username: username
spring.datasource.password: WRONG
spring.datasource.driver-class-name: driver
spring.datasource.type: com.zaxxer.hikari.HikariDataSource

#Following are hikari specific properties
spring.datasource.initializationFailTimeout: 5000
spring.datasource.maximum-pool-size: 10
spring.datasource.minimum-idle: 1
spring.datasource.connection-timeout: 30000
spring.datasource.pool-name: datasource-pool

Note that all these properties are just with prefix spring.datasource (I don't use spring.datasource.hikari from DataSourceAutoConfiguration.class ).

Using DataSourceBuilder for initializing datasource leads to exactly the same behavior as you described (the app throws an exception about the wrong password but continue to launch nevertheless). I confirm that it stops launching only when you don't have custom transaction manager etc.

I'm using Spring Boot 2.1.9 with HikariCP 3.2.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM