【问题标题】:Spring Boot Integration Tests With @MockBean don't release jdbc connections使用 @MockBean 的 Spring Boot 集成测试不释放 jdbc 连接
【发布时间】:2018-07-27 03:41:00
【问题描述】:

我对集成测试有疑问。

我们使用的是 spring boot 1.4.4 + spring batch + testcontainers + postgres。

每个集成测试都有注释:

@RunWith(SpringRunner.class)
@ActiveProfiles(value = { "integrationtest" })
@SpringBootTest(classes = ServiceApplication.class)

问题是:

看起来每个包含@MockBean 注解的集成测试都会创建一个新的上下文。 每个新上下文都会创建一个具有 10 个连接的新连接池。 但是之前的上下文并没有释放它的连接。

因此,在进行此类测试之前,我可以从 postgres 中选择连接数:

select sum(numbackends) from pg_stat_database;

并看到每个测试添加了 10 个新连接。

由于 100 个连接限制,第 10 次测试失败。

您能建议如何解决它吗?

【问题讨论】:

  • 您尝试过@DirtiesContext 吗?检查here
  • 你有一些创建连接池的配置吗?

标签: spring-boot spring-batch integration-testing testcontainers


【解决方案1】:

看起来像注释的组合 + 所有@MockBean 类计算某种“哈希”。

每个“散列”都会创建自己的上下文,spring 会缓存它。 每个包含创建自己的连接池,默认有 10 个连接。

虽然所有上下文都被缓存,但所有连接池都保持连接 - 这就是我的问题的原因。

正如 jusermar10 所说,您可以使用 @DirtiesContext 注释修复它。

【讨论】:

    猜你喜欢
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 2015-01-01
    • 2020-02-25
    相关资源
    最近更新 更多