gpt4 book ai didi

java - testcontainers, hikari 和 Failed to validate connection org.postgresql.jdbc.PgConnection

转载 作者:行者123 更新时间:2023-12-03 11:18:24 31 4
gpt4 key购买 nike

我有一个 Spring 启动应用程序。我正在使用 testcontainers 对其进行测试,以确保数据库(postgres)和存储库实现执行它们应该执行的操作。

我用以下内容初始化容器并且工作得很好。

    @Container
@SuppressWarnings("rawtypes")
private static final PostgreSQLContainer POSTGRE_SQL = new PostgreSQLContainer("postgres:9.6")
.withDatabaseName("xxx")
.withUsername("xxx")
.withPassword("xxx");

static class Initialiser implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of(
"spring.datasource.url=" + POSTGRE_SQL.getJdbcUrl(),
"spring.datasource.username=" + POSTGRE_SQL.getUsername(),
"spring.jpa.hibernate.ddl-auto=create-drop"
).applyTo(applicationContext.getEnvironment());
}
}

问题是,虽然测试成功,但在类(class)结束时,当容器关闭时,我从 hikari 收到以下错误消息
[31mWARN [0;39m [36mcom.zaxxer.hikari.pool.PoolBase.isConnectionAlive[0;39m - HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@4d728138 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
[31mWARN [0;39m [36mcom.zaxxer.hikari.pool.PoolBase.isConnectionAlive[0;39m - HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@43070a2e (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
[31mWARN [0;39m [36mcom.zaxxer.hikari.pool.PoolBase.isConnectionAlive[0;39m - HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@1aa53837 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
[31mWARN [0;39m [36mcom.zaxxer.hikari.pool.PoolBase.isConnectionAlive[0;39m - HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@3d7cffa2 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
[31mWARN [0;39m [36mcom.zaxxer.hikari.pool.PoolBase.isConnectionAlive[0;39m - HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@634e7d8e (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
[31mWARN [0;39m [36mcom.zaxxer.hikari.pool.PoolBase.isConnectionAlive[0;39m - HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@18634db3 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
[31mWARN [0;39m [36mcom.zaxxer.hikari.pool.PoolBase.isConnectionAlive[0;39m - HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@2bb4ba08 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
[31mWARN [0;39m [36mcom.zaxxer.hikari.pool.PoolBase.isConnectionAlive[0;39m - HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@71efd133 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
[31mWARN [0;39m [36mcom.zaxxer.hikari.pool.PoolBase.isConnectionAlive[0;39m - HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@61dd608d (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
[31mWARN [0;39m [36mcom.zaxxer.hikari.pool.PoolBase.isConnectionAlive[0;39m - HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@6351b7d0 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.

他们没有让我的测试失败,我怀疑它的发生是因为容器,因此数据库不再存在,hikari 仍然试图保持连接池处于 Activity 状态。所以测试完成需要几秒钟,而 hikari 正式提示连接失败。

我尝试在 Initialiser 中设置 hikari 属性喜欢 "spring.datasource.hikari.maxLifetime=1""spring.datasource.hikari.idleTimeout=1"没有任何运气。

有什么建议么?

谢谢

最佳答案

我遇到了完全相同的问题:如果我单独运行它,则测试通过,但当我与其他测试一起运行时失败。
我发现 SpringBootTest 在测试之间重用了 Spring 上下文,所以测试之间有一个共同的 Hikari 池。但是在后台 testcontainers 杀死(在上一次测试之后)一个容器并创建了一个新容器(在下一次测试之前)。 SpringBootTest 不知道导致新 Postgres 容器的更改,因此 Hikari Pool 与之前的测试相同(指向已使用且当前不可用的端口)
在我的情况下添加 @DirtiesContext测试注释有帮助。

关于java - testcontainers, hikari 和 Failed to validate connection org.postgresql.jdbc.PgConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59372048/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com