简体   繁体   English

testcontainers、hikari 和无法验证连接 org.postgresql.jdbc.PgConnection

[英]testcontainers, hikari and Failed to validate connection org.postgresql.jdbc.PgConnection

I have a spring boot app.我有一个弹簧启动应用程序。 I'm testing it with testcontainers to ensure that the DB (postgres) and the Repository implementation do what they are supposed to do.我正在使用 testcontainers 对其进行测试,以确保 DB (postgres) 和 Repository 实现执行它们应该执行的操作。

I initialise the container with the following and works pretty well.我使用以下内容初始化容器并且运行良好。

    @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());
        }
    }

The problem is that while the tests are successful, at the end of the class, when the container gets shutdown I get the following error messages from hikari问题是,虽然测试成功,但在课程结束时,当容器关闭时,我从 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.

They are not making my test failing and i suspect it happens because the container, and therefore the db, is no longer there and hikari still tries to keep the connection pool alive.他们没有让我的测试失败,我怀疑这是因为容器和数据库不再存在,而 hikari 仍然试图保持连接池处于活动状态。 so the test to complete takes several seconds while hikari officially complain of failing connection.所以完成测试需要几秒钟,而 hikari 正式抱怨连接失败。

I tried playing with, settings hikari properties in the Initialiser like "spring.datasource.hikari.maxLifetime=1" and "spring.datasource.hikari.idleTimeout=1" without any luck.我尝试在Initialiser设置 hikari 属性,例如"spring.datasource.hikari.maxLifetime=1""spring.datasource.hikari.idleTimeout=1"但没有任何运气。

Any suggestions?有什么建议?

Thank you谢谢

I had the exact same problem: test passed if I run it individually, but failed when I run it together with other tests.我遇到了完全相同的问题:如果我单独运行它,测试通过,但当我与其他测试一起运行时失败。

I found that SpringBootTest is reusing Spring context between tests so there is a common Hikari Pool between tests.我发现 SpringBootTest 在测试之间重用 Spring 上下文,因此在测试之间有一个公共的 Hikari Pool。 But in the background testcontainers killed (after the previous test) a container and created a new one (before the next test).但是在后台 testcontainers 杀死了(在上一个测试之后)一个容器并创建了一个新的(在下一个测试之前)。 SpringBootTest is not aware of that change resulting in a new Postgres container so Hikari Pool is the same as in the previous test (pointing to already used and currently unavailable port) SpringBootTest 不知道导致新 Postgres 容器的更改,因此 Hikari Pool 与之前的测试相同(指向已使用且当前不可用的端口)

In my case adding @DirtiesContext annotation to test helped.在我的情况下,添加@DirtiesContext注释来测试有帮助。

I had the same issue in the question and found that setting the hikari connection timeout of the integration tests helps avoid the delay.我在问题中遇到了同样的问题,发现设置集成测试的 hikari 连接超时有助于避免延迟。

spring:
  datasource:
    hikari:
      connection-timeout: 250

Try other pool尝试其他池

1: Tomcat-jdbc( https://github.com/apache/tomcat/tree/main/modules/jdbc-pool ) 1:Tomcat-jdbc( https://github.com/apache/tomcat/tree/main/modules/jdbc-pool )

2: Druid( https://github.com/alibaba/druid ) 2:德鲁伊( https://github.com/alibaba/druid

3: BeeCP( https://github.com/Chris2018998/BeeCP ), which faster than HikariCP 3:BeeCP( https://github.com/Chris2018998/BeeCP ),比HikariCP快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FINE日志记录org.postgresql.jdbc.PgConnection setAutoCommit = false - FINE logging org.postgresql.jdbc.PgConnection setAutoCommit = false 方法 org.postgresql.jdbc.PgConnection.createClob() 尚未实现 - Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented 如何解决org.postgresql.jdbc.PgConnection.createClob()尚未实现 - How to resolve org.postgresql.jdbc.PgConnection.createClob() is not yet implemented Postgres Error方法org.postgresql.jdbc.PgConnection.createClob()未实现 - Postgres Error method org.postgresql.jdbc.PgConnection.createClob() is not implemented 如何解决“方法 org.postgresql.jdbc.PgConnection.createBlob() 尚未实现” - How to resolve “Method org.postgresql.jdbc.PgConnection.createBlob() is not yet implemented” 如果没有休眠依赖项,则尚未实现org.postgresql.jdbc.PgConnection.createClob()方法 - Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented if hibernate dependency is absent 如何将Jdbc4Connection转换为PGConnection? - How to cast Jdbc4Connection to PGConnection? ClassCastException:org.postgresql.jdbc4.Jdbc4Connection无法转换为org.postgresql.jdbc4.Jdbc4Connection - ClassCastException: org.postgresql.jdbc4.Jdbc4Connection cannot be cast to org.postgresql.jdbc4.Jdbc4Connection PostgreSQL:无法获得JDBC连接 - PostgreSQL:Failed to obtain JDBC Connection Hikari无法获取JDBC连接 - Hikari Unable to acquire JDBC Connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM