Skip to content

Commit 7509acd

Browse files
quaffschauder
authored andcommitted
Destroy docker container properly.
Since JdbcContaieners are AutoClosables Spring will call `close()` on shut down. Original pull request #2379
1 parent affe99a commit 7509acd

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

src/test/java/org/springframework/data/jpa/repository/procedures/MySqlStoredProcedureIntegrationTests.java

+11-15
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
*
6464
* @author Gabriel Basilio
6565
* @author Greg Turnquist
66+
* @author Yanming Zhou
6667
*/
6768
@Transactional
6869
@ExtendWith(SpringExtension.class)
@@ -190,26 +191,21 @@ public interface EmployeeRepositoryWithNoCursor extends JpaRepository<Employee,
190191
@EnableTransactionManagement
191192
static class Config {
192193

193-
private MySQLContainer<?> MYSQL_CONTAINER;
194-
195-
@Bean
196-
public DataSource dataSource() {
197-
198-
if (MYSQL_CONTAINER == null) {
199-
200-
MYSQL_CONTAINER = new MySQLContainer<>("mysql:8.0.24") //
194+
@SuppressWarnings("resource")
195+
@Bean(initMethod = "start")
196+
public MySQLContainer<?> container() {
197+
return new MySQLContainer<>("mysql:8.0.24") //
201198
.withUsername("test") //
202199
.withPassword("test") //
203200
.withConfigurationOverride("");
204-
MYSQL_CONTAINER.start();
205-
}
201+
}
206202

203+
@Bean
204+
public DataSource dataSource(MySQLContainer<?> container) {
207205
MysqlDataSource dataSource = new MysqlDataSource();
208-
dataSource.setUrl(MYSQL_CONTAINER.getJdbcUrl());
209-
dataSource.setUser("root");
210-
dataSource.setPassword(MYSQL_CONTAINER.getPassword());
211-
dataSource.setDatabaseName(MYSQL_CONTAINER.getDatabaseName());
212-
206+
dataSource.setUrl(container.getJdbcUrl());
207+
dataSource.setUser(container.getUsername());
208+
dataSource.setPassword(container.getPassword());
213209
return dataSource;
214210
}
215211

src/test/java/org/springframework/data/jpa/repository/procedures/PostgresStoredProcedureIntegrationTests.java

+11-14
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
*
6666
* @author Gabriel Basilio
6767
* @author Greg Turnquist
68+
* @author Yanming Zhou
6869
*/
6970
@Transactional
7071
@ExtendWith(SpringExtension.class)
@@ -193,23 +194,19 @@ public interface EmployeeRepositoryWithRefCursor extends JpaRepository<Employee,
193194
@EnableTransactionManagement
194195
static class Config {
195196

196-
private PostgreSQLContainer<?> POSTGRESQL_CONTAINER;
197+
@SuppressWarnings("resource")
198+
@Bean(initMethod = "start")
199+
public PostgreSQLContainer<?> container() {
200+
return new PostgreSQLContainer<>("postgres:9.6.12") //
201+
.withUsername("postgres");
202+
}
197203

198204
@Bean
199-
public DataSource dataSource() {
200-
201-
if (POSTGRESQL_CONTAINER == null) {
202-
203-
POSTGRESQL_CONTAINER = new PostgreSQLContainer<>("postgres:9.6.12") //
204-
.withUsername("postgres");
205-
POSTGRESQL_CONTAINER.start();
206-
}
207-
205+
public DataSource dataSource(PostgreSQLContainer<?> container) {
208206
PGSimpleDataSource dataSource = new PGSimpleDataSource();
209-
dataSource.setUrl(POSTGRESQL_CONTAINER.getJdbcUrl());
210-
dataSource.setUser(POSTGRESQL_CONTAINER.getUsername());
211-
dataSource.setPassword(POSTGRESQL_CONTAINER.getPassword());
212-
207+
dataSource.setUrl(container.getJdbcUrl());
208+
dataSource.setUser(container.getUsername());
209+
dataSource.setPassword(container.getPassword());
213210
return dataSource;
214211
}
215212

0 commit comments

Comments
 (0)