title: Spring with Testcontainers not returning connections to pool
body: <p>I'm trying to perform integration tests on my DAO using <code>Testcontainers</code>.
  My target is to run every test in separate container. For this purpose I've added the <code>@DirtiesContext</code> annotation - my logic was that if I inject new <code>EntityManager</code> on every test it would have an updated <code>DataSource</code>. And yes its working practically fine, <strong>but</strong> when it comes to the 11th test I get an exception that Hikari can't create new connection cause connection limit of is reached.</p>
  <p>I suppose that when app performs <code>@DirtiesContext</code> current <code>EntityManager</code> is pushed away from the app context and get no longer managed by Spring, so it may &quot;forget&quot; to close the connection.
  Or maybe it's a problem with <code>Testcontainers</code>?</p>
  <p>Here is my code:</p>
  <p>JpaUserDao.java:</p>
  <pre class="lang-java prettyprint-override"><code>@Repository
  public class JpaUserDao implements UserDao {
  
      @PersistenceContext
      private EntityManager entityManager;
  
      @Transactional
      @Override
      public long add(User user) {
          entityManager.persist(user);
          return user.getId();
      }
  //other DAO methods
  }
  </code></pre>
  <p>JpaUserDaoTest.java:</p>
  <pre class="lang-java prettyprint-override"><code>
  @ExtendWith(SpringExtension.class)
  @ContextConfiguration(classes = ApplicationConfigurationTest.class)
  @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
  @Testcontainers
  class JpaUserDaoTest {
  
      @Container
      private PostgreSQLContainer&lt;?&gt; postgreSqlContainer = new PostgreSQLContainer&lt;&gt;(&quot;postgres:13.0-alpine&quot;)
              .withDatabaseName(&quot;example&quot;)
              .withUsername(&quot;test&quot;)
              .withPassword(&quot;test&quot;);
  
      @Autowired
      private UserDao userDao;
  
      private User user;
  
      @BeforeEach
      void set(){
          user = new User();
        //set parameters of User
      }
  
      @Test
      void addShouldAddUserToDatabase() {
          long id = userDao.add(user);
  
          assertEquals(1, id);
      }
  
  //other tests
  }
  </code></pre>
  <p>ApplicationConfigurationTest.java:</p>
  <pre class="lang-java prettyprint-override"><code>@ComponentScan(basePackages = &quot;com.example&quot;)
  @Configuration
  @EnableTransactionManagement
  public class ApplicationConfigurationTest {
  
      public static final String PACKAGES_TO_SCAN = &quot;com.example.model&quot;;
      private static final String JDBC_CONFIGURATION_FILE = &quot;src/test/resources/jdbc.properties&quot;;
      private static final String HIBERNATE_CONFIGURATION_FILE = &quot;src/test/resources/hibernate.properties&quot;;
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
          LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
          entityManagerFactoryBean.setDataSource(dataSource());
          entityManagerFactoryBean.setPackagesToScan(PACKAGES_TO_SCAN);
          entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());
          entityManagerFactoryBean.setJpaProperties(additionalProperties());
          return entityManagerFactoryBean;
      }
  
      private DataSource dataSource(){
          HikariConfig hikariConfig = new HikariConfig(JDBC_CONFIGURATION_FILE);
          return new HikariDataSource(hikariConfig);
      }
  
      private JpaVendorAdapter jpaVendorAdapter() {
          return new HibernateJpaVendorAdapter();
      }
  
      @Bean
      public PlatformTransactionManager transactionManager() {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
          return transactionManager;
      }
  
      private Properties additionalProperties() {
           try (InputStream input = new FileInputStream(HIBERNATE_CONFIGURATION_FILE)) {
              Properties properties = new Properties();
              properties.load(input);
               return properties;
          } catch (IOException ex) {
              throw new PropertiesLoadException();
          }
      }
  }
  </code></pre>
  <p>hibernate.properties:</p>
  <pre><code>hibernate.show_sql=true
  hibernate.hbm2ddl.auto=create
  hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect
  </code></pre>
  <p>jdbc.properties:</p>
  <pre><code>jdbcUrl=jdbc:tc:postgresql:13.0:///example
  dataSource.user=test
  dataSource.password=test
  dataSource.cachePrepStmts=true
  dataSource.prepStmtCacheSize=250
  dataSource.prepStmtCacheSqlLimit=2048
  </code></pre>
  <p>And, finally, the exception itself:</p>
  <pre><code>java.lang.IllegalStateException: Failed to load ApplicationContext
  
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
      at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
      at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
      at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:98)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$6(ClassBasedTestDescriptor.java:350)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:355)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$7(ClassBasedTestDescriptor.java:350)
      at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
      at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
      at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
      at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
      at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
      at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312)
      at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:735)
      at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:734)
      at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:349)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$instantiateAndPostProcessTestInstance$4(ClassBasedTestDescriptor.java:270)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:269)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$2(ClassBasedTestDescriptor.java:259)
      at java.base/java.util.Optional.orElseGet(Optional.java:362)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$3(ClassBasedTestDescriptor.java:258)
      at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:31)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare$0(TestMethodTestDescriptor.java:101)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:100)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:65)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$1(NodeTestTask.java:111)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:111)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:79)
      at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
      at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
      at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
      at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
      at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
      at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
      at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
      at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
      at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
      at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in com.vironit.onlinepharmacy.dao.jpa.ApplicationConfigurationTest: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: Could not create new connection
      at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:483)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1109)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
      at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:127)
      at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
      at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:275)
      at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:243)
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
      ... 67 more
  Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: Could not create new connection
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650)
      ... 85 more
  Caused by: com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: Could not create new connection
      at com.zaxxer.hikari.pool.HikariPool.throwPoolInitializationException(HikariPool.java:595)
      at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:581)
      at com.zaxxer.hikari.pool.HikariPool.&lt;init&gt;(HikariPool.java:115)
      at com.zaxxer.hikari.HikariDataSource.&lt;init&gt;(HikariDataSource.java:81)
      at com.example.dao.jpa.ApplicationConfigurationTest.dataSource(ApplicationConfigurationTest.java:42)
      at com.example.dao.jpa.ApplicationConfigurationTest.entityManagerFactory(ApplicationConfigurationTest.java:33)
      at com.example.dao.jpa.ApplicationConfigurationTest$$EnhancerBySpringCGLIB$$8099423b.CGLIB$entityManagerFactory$0(&lt;generated&gt;)
      at com.example.dao.jpa.ApplicationConfigurationTest$$EnhancerBySpringCGLIB$$8099423b$$FastClassBySpringCGLIB$$12185da1.invoke(&lt;generated&gt;)
      at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
      at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
      at com.example.dao.jpa.ApplicationConfigurationTest$$EnhancerBySpringCGLIB$$8099423b.entityManagerFactory(&lt;generated&gt;)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:564)
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
      ... 86 more
  Caused by: java.sql.SQLException: Could not create new connection
      at org.testcontainers.containers.JdbcDatabaseContainer.createConnection(JdbcDatabaseContainer.java:220)
      at org.testcontainers.jdbc.ContainerDatabaseDriver.connect(ContainerDatabaseDriver.java:124)
      at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:121)
      at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358)
      at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
      at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477)
      at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560)
      ... 100 more
  Caused by: org.postgresql.util.PSQLException: FATAL: sorry, too many clients already
      at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:613)
      at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:161)
      at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213)
      at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
      at org.postgresql.jdbc.PgConnection.&lt;init&gt;(PgConnection.java:225)
      at org.postgresql.Driver.makeConnection(Driver.java:465)
      at org.postgresql.Driver.connect(Driver.java:264)
      at org.testcontainers.containers.JdbcDatabaseContainer.createConnection(JdbcDatabaseContainer.java:211)
      ... 106 more
  
  </code></pre>