【问题标题】:Checking MySQL DB connectivity with Spring Boot JPA - how to set the timeout of Hikari使用 Spring Boot JPA 检查 MySQL DB 连接 - 如何设置 Hikari 的超时
【发布时间】:2020-04-11 22:29:33
【问题描述】:

我的 Spring Boot 应用程序正在使用 MySQL 数据库,我想添加一个检查以了解应用程序是否可以访问数据库。比如:

Select 1 From Cats

应用使用 JPA 来连接数据库并执行不同的业务逻辑需求:

public interface CatRepository extends CrudRepository<Cat, Long>{
      // nothing here at the moment
}

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-jpa</artifactId>
     <version>2.2.0.RELEASE</version>
</dependency>

由于在CrudRepository 中没有与ping 类似的东西,我正在使用count() 方法:

 public boolean isDBReachable(){
    long count = catRepository.count();
    return true;
}

所以当DB不可达时,会抛出如下异常:

ERROR [o.h.e.jdbc.spi.SqlExceptionHelper] - HikariPool-1 - Connection is not available, request timed out after 30002ms. 

我喜欢将超时时间从 30 秒更改为 5 秒。怎么办?

【问题讨论】:

    标签: hibernate spring-boot jpa spring-data-jpa hikaricp


    【解决方案1】:

    post 的帮助下,解决方案是更新应用程序的 yml 文件:

    spring:
      datasource:
         hikari:
             connection-timeout: 5000
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2018-10-05
      • 2019-04-17
      • 2018-10-22
      • 1970-01-01
      • 2015-06-16
      • 2021-08-16
      • 1970-01-01
      • 2019-12-25
      相关资源
      最近更新 更多