java 使用连接池进行 Spring 启动和数据库测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39246795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:12:21  来源:igfitidea点击:

Spring boot and Database testing with connection pool

javadatabasespringspring-bootconnection-pooling

提问by dilbert elbonia

I am trying to create tests for my app which connects to a database. The DataSource is a conection pool (Hikari).

我正在尝试为连接到数据库的应用程序创建测试。DataSource 是一个连接池(Hikari)。

Here is my test configuration:

这是我的测试配置:

@Configuration
public class SqlTestConfig {

    @Bean
    DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setMaximumPoolSize(2);
        config.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        config.setJdbcUrl("jdbc:sqlserver://serversql:1433;database=myDatabase");
        config.setUsername("user");
        config.setPassword("password");
        return new HikariDataSource(config);
    }
}

Here is my test class:

这是我的测试课:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SqlTestConfig.class)
@Slf4j
@Sql(
        scripts = "/clearTables.sql",
        config = @SqlConfig(separator = "GO")
)
public class SqlTest {

    @Autowired
    DataSource dataSource;

    @Test
    public void test1() throws SQLException {
        log.info("catalog:" + dataSource.getConnection().getCatalog());
    }

    @Test
    public void test2() throws SQLException {
        log.info("catalog:" + dataSource.getConnection().getCatalog());
    }

    @Test
    public void test3() throws SQLException {
        log.info("catalog:" + dataSource.getConnection().getCatalog());
    }

    @Test
    public void test4() throws SQLException {
        log.info("catalog:" + dataSource.getConnection().getCatalog());
    }
}

Notice that the MaximumPoolSize is set to 2. When I run the test class the first two tests are successfully completed and the remaining tests fail because the pool gets depleted of connections (connection timeout).

请注意,MaximumPoolSize 设置为 2。当我运行测试类时,前两个测试成功完成,其余测试失败,因为池中的连接耗尽(连​​接超时)。

I believe the problem is because of the @Sql annotation which causes DataSourceInitializer -sto be created to execute the cleanup script but the connections are never returned to the pool.

我相信问题是因为 @Sql 注释导致DataSourceInitializer -s创建以执行清理脚本,但连接永远不会返回到池中。

When I set MaximumPoolSize to 4 all tests are successfully completed. I cannot tell if I have made a configuration error or if this is a bug in Spring.

当我将 MaximumPoolSize 设置为 4 时,所有测试都成功完成。我不知道我是否犯了配置错误,或者这是否是 Spring 中的错误。

采纳答案by miensol

The getConnectionacquires connection from underlying pool. Change your tests to properly close the acquired connection like so:

getConnection获取从资产池的连接。更改您的测试以正确关闭获取的连接,如下所示:

@Test
public void test1() throws SQLException {
    try (Connection connection = dataSource.getConnection()) {
        log.info("catalog:" + connection.getCatalog());
    }
}