[英]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.