From 7509acde8756ed7a03b087352d1c8f46a7caef41 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Mon, 6 Dec 2021 15:05:27 +0800 Subject: [PATCH] Destroy docker container properly. Since JdbcContaieners are AutoClosables Spring will call `close()` on shut down. Original pull request #2379 --- .../MySqlStoredProcedureIntegrationTests.java | 26 ++++++++----------- ...stgresStoredProcedureIntegrationTests.java | 25 ++++++++---------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/test/java/org/springframework/data/jpa/repository/procedures/MySqlStoredProcedureIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/procedures/MySqlStoredProcedureIntegrationTests.java index 2bde92038..53fccd746 100644 --- a/src/test/java/org/springframework/data/jpa/repository/procedures/MySqlStoredProcedureIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/procedures/MySqlStoredProcedureIntegrationTests.java @@ -63,6 +63,7 @@ import com.mysql.cj.jdbc.MysqlDataSource; * * @author Gabriel Basilio * @author Greg Turnquist + * @author Yanming Zhou */ @Transactional @ExtendWith(SpringExtension.class) @@ -190,26 +191,21 @@ public class MySqlStoredProcedureIntegrationTests { @EnableTransactionManagement static class Config { - private MySQLContainer MYSQL_CONTAINER; - - @Bean - public DataSource dataSource() { - - if (MYSQL_CONTAINER == null) { - - MYSQL_CONTAINER = new MySQLContainer<>("mysql:8.0.24") // + @SuppressWarnings("resource") + @Bean(initMethod = "start") + public MySQLContainer container() { + return new MySQLContainer<>("mysql:8.0.24") // .withUsername("test") // .withPassword("test") // .withConfigurationOverride(""); - MYSQL_CONTAINER.start(); - } + } + @Bean + public DataSource dataSource(MySQLContainer container) { MysqlDataSource dataSource = new MysqlDataSource(); - dataSource.setUrl(MYSQL_CONTAINER.getJdbcUrl()); - dataSource.setUser("root"); - dataSource.setPassword(MYSQL_CONTAINER.getPassword()); - dataSource.setDatabaseName(MYSQL_CONTAINER.getDatabaseName()); - + dataSource.setUrl(container.getJdbcUrl()); + dataSource.setUser(container.getUsername()); + dataSource.setPassword(container.getPassword()); return dataSource; } diff --git a/src/test/java/org/springframework/data/jpa/repository/procedures/PostgresStoredProcedureIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/procedures/PostgresStoredProcedureIntegrationTests.java index ae6b42366..b67a53b6f 100644 --- a/src/test/java/org/springframework/data/jpa/repository/procedures/PostgresStoredProcedureIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/procedures/PostgresStoredProcedureIntegrationTests.java @@ -65,6 +65,7 @@ import org.testcontainers.containers.PostgreSQLContainer; * * @author Gabriel Basilio * @author Greg Turnquist + * @author Yanming Zhou */ @Transactional @ExtendWith(SpringExtension.class) @@ -193,23 +194,19 @@ public class PostgresStoredProcedureIntegrationTests { @EnableTransactionManagement static class Config { - private PostgreSQLContainer POSTGRESQL_CONTAINER; + @SuppressWarnings("resource") + @Bean(initMethod = "start") + public PostgreSQLContainer container() { + return new PostgreSQLContainer<>("postgres:9.6.12") // + .withUsername("postgres"); + } @Bean - public DataSource dataSource() { - - if (POSTGRESQL_CONTAINER == null) { - - POSTGRESQL_CONTAINER = new PostgreSQLContainer<>("postgres:9.6.12") // - .withUsername("postgres"); - POSTGRESQL_CONTAINER.start(); - } - + public DataSource dataSource(PostgreSQLContainer container) { PGSimpleDataSource dataSource = new PGSimpleDataSource(); - dataSource.setUrl(POSTGRESQL_CONTAINER.getJdbcUrl()); - dataSource.setUser(POSTGRESQL_CONTAINER.getUsername()); - dataSource.setPassword(POSTGRESQL_CONTAINER.getPassword()); - + dataSource.setUrl(container.getJdbcUrl()); + dataSource.setUser(container.getUsername()); + dataSource.setPassword(container.getPassword()); return dataSource; }