【发布时间】: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