Spring boot 如果数据库关闭,Spring引导API将挂起

Spring boot 如果数据库关闭,Spring引导API将挂起,spring-boot,docker,jpa,docker-compose,Spring Boot,Docker,Jpa,Docker Compose,运行2个docker容器 连接到Postgres数据库的Spring Boot2.4.0 API 现在尝试测试Postgres何时关闭/失败 为此,我使用命令docker stop postgres停止容器 现在,当我向API发送请求时,我希望返回一个错误 但是客户端(邮递员)只是挂起 日志显示: training-data-api | 2020-11-26 14:47:55.248 WARN 1 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.

运行2个
docker
容器

连接到
Postgres
数据库的
Spring Boot
2.4.0 API

现在尝试测试
Postgres
何时关闭/失败

为此,我使用命令
docker stop postgres
停止容器

现在,当我向API发送请求时,我希望返回一个错误

但是客户端(
邮递员
)只是挂起

日志显示:

training-data-api    | 2020-11-26 14:47:55.248  WARN 1 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@21f0deb5 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
training-data-api    | 2020-11-26 14:47:55.254  WARN 1 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@78e73221 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
training-data-api    | 2020-11-26 14:47:55.255  WARN 1 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@5fc55efe (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
training-data-api    | 2020-11-26 14:47:55.256  WARN 1 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@3bb3a3db (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
training-data-api    | 2020-11-26 14:47:55.261  WARN 1 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@7bde48e5 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
training-data-api    | 2020-11-26 14:47:55.264  WARN 1 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@3a691b85 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
training-data-api    | 2020-11-26 14:47:55.266  WARN 1 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@4fae8098 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
training-data-api    | 2020-11-26 14:47:55.272  WARN 1 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@69254fcd (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
training-data-api    | 2020-11-26 14:47:55.273  WARN 1 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@60b5ab6c (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
training-data-api    | 2020-11-26 14:47:55.274  WARN 1 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@127231cc (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
training-data-api    | 2020-11-26 14:48:25.246  WARN 1 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 08001
training-data-api    | 2020-11-26 14:48:25.247 ERROR 1 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper   : HikariPool-1 - Connection is not available, request timed out after 30001ms.
training-data-api    | 2020-11-26 14:48:25.247 ERROR 1 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper   : The connection attempt failed.
API为什么不发送响应,有什么提示或想法吗

我的属性文件

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://postgres:5432/postgres
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.initialization-mode=always
spring.datasource.initialize=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
已尝试将这些添加到属性,但没有更改:

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL95Dialect
spring.datasource.continue-on-error=true

您的日志显示“请求在30001ms后超时”。这意味着连接超时设置为30秒。您的意思是API即使在30秒超时后也不会返回错误吗?