使用Spring Boot JPA检查MySQL数据库连接-如何设置Hikari的超时时间

问题描述 投票:0回答:1

我的Spring Boot应用程序正在使用MySQL DB,我想添加一张支票以了解该应用程序是否可以访问该数据库。类似于:

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

因此,当数据库不可访问时,将引发以下异常:

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个回答
0
投票

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

spring:
  datasource:
     hikari:
         connection-timeout: 5000
© www.soinside.com 2019 - 2024. All rights reserved.