mysqlspring-bootjpahikaricpreconnect

Hikaripool JPARepository not reconnect/recreate connections after database Mysql restart


Im using SpringBoot 2.2.10, Hikari to manage connection pool and using JPA to query from database Mysql to create a microservice. There is one testing scenario which my microservice is supposed to functionalize after database restart/reboot, but no success. I did try to find solutions on internet and SO and there are many similar questions, some even were asked years ago but im totally a noob because not single one works in my situation. Below goes my configurations and stacktraces

spring.datasource.jdbcUrl=jdbc:mysql://xxx.xxx.xxx.xxx:3306/core
spring.datasource.username=admin
spring.datasource.password=1
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.maximumPoolSize=10
spring.datasource.idleTimeout=30000
spring.datasource.maxLifeTime=1800000
spring.datasource.connectionTestQuery=SELECT 1 FROM DUAL

and my Datasource Config class:

@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class HikariCoreConfig extends HikariConfig {

     @Bean
     @public DataSource dataSource() {
         return new HikariDataSource(this);
     }
}

On startup, my application works fine, reads and writes normally from db. After i kill then start db process again, whenever i write to db (using repository.save(object)), i keep getting exceptions:

CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is ...JDBCConnectionException: Unable to acquire JDBC Connection

(I assure that mysql is running, i can connect and query through Workbench from my localmachine)

Here are full stacktrace

enter image description here

enter image description here

enter image description here

(sorry i cant provide in text)

I read many sites on the internet and people say that Hikari handles the reconnect things automatically. Do i do it wrong or i miss something? Please help, thanks!


Solution

  • i ve found the problems. Hikari does reconnect, all my configurations is set properly. My situation happens when i set keystore/truststore in the system property by using :

       System.setProperty("javax.net.ssl.keyStore", keyStorePath);
       System.setProperty("javax.net.ssl.keyStorePassword", kspassword);
    
       System.setProperty("javax.net.ssl.trustStore", trustStorePath);
       System.setProperty("javax.net.ssl.trustStorePassword", tspassword);
    

    It makes the connection not usable after db restarts. The solution for that is simply put two params in jdbcurl: &useSSL=false&allowPublicKeyRetrieval=true:

       spring.datasource.jdbcUrl=jdbc:mysql://xxx.xxx.xxx.xxx:3306/core?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
    

    That will do.