I have the following Singleton Container as described in Testcontainers Lifecycle
public abstract class AbstractPostgresIT {
static PostgreSQLContainer<?> POSTGRES_CONTAINER =
new PostgreSQLContainer<>(DockerImageName.parse("postgres:16")).withDatabaseName("ems")
.withUsername("username").withPassword("password");
static {
POSTGRES_CONTAINER.start();
}
@DynamicPropertySource
public static void dynamicPropertySource(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", POSTGRES_CONTAINER::getJdbcUrl);
registry.add("spring.datasource.username", POSTGRES_CONTAINER::getUsername);
registry.add("spring.datasource.password", POSTGRES_CONTAINER::getPassword);
registry.add("spring.jpa.hibernate.ddl-auto", () -> "create-drop");
}
}
I have two classes extending AbstractPostgresIT. Sometimes I get an error as
HikariPool-2 - Failed to validate connection org.postgresql.jdbc.PgConnection@4ec6f11d (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
I wonder I try to understand if this approach dirties my context by confusing the cached HikariPool by switching databases. Then I would add @DirtiesContext to AbstractPostgresIT. But I’m not sure about this. So does setting spring.jpa.hibernate.ddl-auto=create-drop dirties my context?