首页 > 解决方案 > (此连接已关闭。)。使用 hikari 连接 PostgreSQL 时可能考虑使用更短的 maxLifetime 值

问题描述

我在 Spring Boot 项目中使用 Hikari 连接 PostgreSQL 13 数据库,现在日志显示以下警告:

[12:23:49:633] [WARN] - com.zaxxer.hikari.pool.PoolBase.isConnectionAlive(PoolBase.java:158) - HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@a54a5357 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
[12:23:49:634] [WARN] - com.zaxxer.hikari.pool.PoolBase.isConnectionAlive(PoolBase.java:158) - HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@5d8a4eb4 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
[12:23:49:636] [WARN] - com.zaxxer.hikari.pool.PoolBase.isConnectionAlive(PoolBase.java:158) - HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@18564799 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.

第一步,我idle_in_transaction_session_timeout像这样调整 PostgreSQL 13 数据库参数:

alter system set idle_in_transaction_session_timeout='30min';
show idle_in_transaction_session_timeout;

我确保将 PostgreSQL 13 数据库idle_in_transaction_session_timeout参数设置为 30 分钟。下一步我application.properties像这样调整我的 Hikari 的 maxLifeTime:

spring.datasource.hikari.max-lifetime=900000

Hikari maxLifeTime 已经小于数据库连接空闲连接时间,但是警告信息并没有消失。我错过了什么吗?我应该怎么做才能修复警告消息?

标签: javaspringpostgresql

解决方案


推荐阅读