【问题标题】:Hikaripool JPARepository not reconnect/recreate connections after database Mysql restartHikaripool JPARepository 在数据库 Mysql 重新启动后不会重新连接/重新创建连接
【发布时间】:2022-01-25 01:08:59
【问题描述】:

我使用 SpringBoot 2.2.10,Hikari 管理连接池并使用 JPA 从数据库 Mysql 中查询以创建微服务。有一个测试场景,我的微服务应该在数据库重启/重启后运行,但没有成功。我确实尝试在互联网和 SO 上找到解决方案,并且有很多类似的问题,有些甚至在几年前就被问到了,但我完全是一个菜鸟,因为在我的情况下没有一个人可以工作。下面是我的配置和堆栈跟踪

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

和我的数据源配置类:

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

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

在启动时,我的应用程序工作正常,从 db 读取和写入正常。在我杀死然后再次启动 db 进程之后,每当我写入 db(使用repository.save(object))时,我都会不断收到异常:

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

(我保证mysql正在运行,我可以从本地机器通过Workbench进行连接和查询)

这里是完整的堆栈跟踪

(对不起,我无法提供文字)

我在互联网上阅读了许多网站,人们说 Hikari 会自动处理重新连接的事情。我做错了什么还是我错过了什么?请帮忙,谢谢!

【问题讨论】:

    标签: mysql spring-boot jpa hikaricp reconnect


    【解决方案1】:

    我发现了问题。 Hikari 确实重新连接,我的所有配置都已正确设置。我的情况发生在我使用以下方法在系统属性中设置密钥库/信任库时:

       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);
    

    它使连接在数据库重新启动后不可用。解决方法就是在 jdbcurl 中放两个参数:&useSSL=false&allowPublicKeyRetrieval=true:

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

    这样就可以了。

    【讨论】:

      猜你喜欢
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 1970-01-01
      • 2012-07-03
      • 2011-02-02
      • 2018-06-14
      • 1970-01-01
      相关资源
      最近更新 更多