Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgreSQL connection timeout with Kotlin #96

Closed
brunosc opened this issue Feb 21, 2022 · 2 comments
Closed

PostgreSQL connection timeout with Kotlin #96

brunosc opened this issue Feb 21, 2022 · 2 comments

Comments

@brunosc
Copy link

brunosc commented Feb 21, 2022

Hello!

My integration tests were working fine but I decided to switch the codebase to Kotlin and now I'm getting some PostgreSQL errors. When I run the tests for only one controller it works well but when I run mvn test I got the error.

Any idea? Below are the error and the AbstractIntegrationTest

Thanks.

Error

c.z.hikari.pool.PoolBase  | HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@6fe40fa8 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
.e.j.s.SqlExceptionHelper | SQL Error: 0, SQLState: 08001
.e.j.s.SqlExceptionHelper | HikariPool-1 - Connection is not available, request timed out after 30008ms.
.e.j.s.SqlExceptionHelper | Connection to localhost:49153 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 32.441 s <<< FAILURE! - in info.mtgmatches.adapter.web.MessageControllerTest
[ERROR] shouldSendMessage  Time elapsed: 30.056 s  <<< ERROR!
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
Caused by: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
Caused by: java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30008ms.
Caused by: org.postgresql.util.PSQLException: Connection to localhost:49153 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Caused by: java.net.ConnectException: Connection refused

AbstractIntegrationTest

@ActiveProfiles("integration-test")
@Testcontainers(disabledWithoutDocker = true)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
abstract class AbstractIntegrationTest {

  // omitted code

  companion object {
        @Container
        var database: PostgreSQLContainer<*> = PostgreSQLContainer<Nothing>("postgres:12.3").apply {
            withDatabaseName("mydatabasename")
            withUsername("myusername")
            withPassword("mypassword")
        }

        @JvmStatic
        @DynamicPropertySource
        fun setProperties(registry: DynamicPropertyRegistry) {
            registry.add("spring.datasource.url", database::getJdbcUrl)
            registry.add("spring.datasource.password", database::getPassword)
            registry.add("spring.datasource.username", database::getUsername)
        }

        init {
            database.start()
        }
    }

}

Controller

internal class MyControllerTest : AbstractIntegrationTest() {
  // omitted code
}
@rieckpil
Copy link
Owner

Hi @brunosc, migrating to Kotlin is a nice move 👍

I haven't used Kotlin with Testcontainers for quite some time (I used it in the past and also wrote an article about it) but based on what I can see from your AbstractIntegrationTest is that you're mixing a controlled container lifecycle (using @Testcontainers) with a manual lifecycle (calling start/stop on your own).

If running one test works and running them all together fails with a Connection refuse, the issue must be in the lifecycle management of the container. Can you monitor your tests and especially docker ps to see how many PostgreSQL databases are started?

@brunosc
Copy link
Author

brunosc commented Feb 22, 2022

The problem was the @Container, this annotation ensures to stop the container after each test.

I don't know why I put this annotation :)

Thank you anyway!

@brunosc brunosc closed this as completed Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants