Skip to content

Timeout - java.sql.SQLTransientConnectionException: HikariPool-1 #1918

@AleksandarAleksandrov

Description

@AleksandarAleksandrov

Hi I have the following base test class:

import javax.persistence.EntityManager;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;
import org.testcontainers.containers.PostgreSQLContainer;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@TestPropertySource(locations = "classpath:application-test.properties")
public abstract class DbEnabledTest {
  
  @Autowired
  protected EntityManager em;
  
  @Autowired
  protected PlatformTransactionManager platformTransactionManager;
    
  public static PostgreSQLContainer<?> sqlContainer;

  @BeforeClass
  public static void setupEnv() {    
    sqlContainer = new PostgreSQLContainer<>("postgres:11")
      .withDatabaseName("test_scenarios")
      .withUsername("postgres")
      .withPassword("postgres");
    
    sqlContainer.start();

    System.setProperty("spring.datasource.url", sqlContainer.getJdbcUrl());
    System.setProperty("spring.datasource.username", sqlContainer.getUsername());
    System.setProperty("spring.datasource.password", sqlContainer.getPassword());
    System.out.println("Running DB test with - " + sqlContainer.getJdbcUrl());
  }

  @AfterClass
  public static void teardownDbContainer() {
    sqlContainer.stop();
  }
  
  @Before
  public void initDb() {
      new TransactionTemplate(platformTransactionManager).execute(a -> {
          initDBForEachTestMethod();
          return a;
      });
  }
  
  public void initDBForEachTestMethod() {
    System.out.println("Default db with no data!");
  }

  @After
  public void truncateDb() {
      new TransactionTemplate(platformTransactionManager).execute(a -> {
          em.createNativeQuery("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'")
              .getResultList()
              .forEach(table -> {
                  em.createNativeQuery("TRUNCATE TABLE " + table + " CASCADE").executeUpdate();
              });

          return a;
      });
  }

}

When I run the tests in a single class that extends this one, I have no problem, but when I run all of the tests I get the following error:

2019-09-26 18:34:38.851 ERROR 106968 --- [    Test worker] org.hibernate.engine.jdbc.spi.SqlExceptionHelper : HikariPool-1 - Connection is not available, request timed out after 30000ms.

And every test that follows this exception will fail. To me it seems as the connection pool is empty and something is leaking connections, although I have clean logic in @AfterClass.

Any ideas ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions