diff --git a/embedded-database-spring-test/src/main/java/io/zonky/test/db/context/DefaultDatabaseContext.java b/embedded-database-spring-test/src/main/java/io/zonky/test/db/context/DefaultDatabaseContext.java index 97dde25..8aebb43 100644 --- a/embedded-database-spring-test/src/main/java/io/zonky/test/db/context/DefaultDatabaseContext.java +++ b/embedded-database-spring-test/src/main/java/io/zonky/test/db/context/DefaultDatabaseContext.java @@ -221,9 +221,9 @@ public synchronized void destroy() { logger.trace("Closing database context bean - context={}", beanName); if (database != null) { try { - awaitDatabase().close(); + awaitDatabase().shutdown(); } catch (Throwable t) { - // TODO: do nothing - consider logging the error + logger.error("There was a error while shutting down database", t); } } } diff --git a/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/EmbeddedDatabase.java b/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/EmbeddedDatabase.java index 2d0674c..5091b49 100644 --- a/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/EmbeddedDatabase.java +++ b/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/EmbeddedDatabase.java @@ -25,4 +25,5 @@ public interface EmbeddedDatabase extends DataSource, Closeable { void close(); + void shutdown(); } diff --git a/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/postgres/DockerPostgresDatabaseProvider.java b/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/postgres/DockerPostgresDatabaseProvider.java index 1f436c5..a2ca5e6 100644 --- a/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/postgres/DockerPostgresDatabaseProvider.java +++ b/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/postgres/DockerPostgresDatabaseProvider.java @@ -257,7 +257,7 @@ private EmbeddedDatabase getDatabase(ClientConfig config, String dbName) throws dataSource.setProperty(entry.getKey(), entry.getValue()); } - return new BlockingDatabaseWrapper(new PostgresEmbeddedDatabase(dataSource, () -> dropDatabase(config, dbName)), semaphore); + return new BlockingDatabaseWrapper(new PostgresEmbeddedDatabase(dataSource, () -> dropDatabase(config, dbName), container::close), semaphore); } } diff --git a/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/postgres/PostgresEmbeddedDatabase.java b/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/postgres/PostgresEmbeddedDatabase.java index 223f6fd..2e83f96 100644 --- a/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/postgres/PostgresEmbeddedDatabase.java +++ b/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/postgres/PostgresEmbeddedDatabase.java @@ -26,11 +26,15 @@ public class PostgresEmbeddedDatabase extends AbstractEmbeddedDatabase { private final PGSimpleDataSource dataSource; - public PostgresEmbeddedDatabase(PGSimpleDataSource dataSource, Runnable closeCallback) { - super(closeCallback); + public PostgresEmbeddedDatabase(PGSimpleDataSource dataSource, Runnable closeCallback, Runnable shutdownCallback) { + super(closeCallback, shutdownCallback); this.dataSource = dataSource; } + public PostgresEmbeddedDatabase(PGSimpleDataSource dataSource, Runnable closeCallback) { + this(dataSource, closeCallback, null); + } + @Override protected DataSource getDataSource() { return dataSource; diff --git a/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/support/AbstractEmbeddedDatabase.java b/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/support/AbstractEmbeddedDatabase.java index 178ef82..2279e9a 100644 --- a/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/support/AbstractEmbeddedDatabase.java +++ b/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/support/AbstractEmbeddedDatabase.java @@ -2,6 +2,7 @@ import io.zonky.test.db.provider.EmbeddedDatabase; +import javax.annotation.Nullable; import javax.sql.DataSource; import java.io.PrintWriter; import java.sql.Connection; @@ -12,9 +13,15 @@ public abstract class AbstractEmbeddedDatabase implements EmbeddedDatabase { private final Runnable closeCallback; + private final Runnable shutdownCallback; protected AbstractEmbeddedDatabase(Runnable closeCallback) { + this(closeCallback, null); + } + + protected AbstractEmbeddedDatabase(Runnable closeCallback,Runnable shutdownCallback) { this.closeCallback = closeCallback; + this.shutdownCallback = shutdownCallback; } protected abstract DataSource getDataSource(); @@ -80,4 +87,9 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException { public synchronized void close() { closeCallback.run(); } + + @Override + public void shutdown() { + shutdownCallback.run(); + } } diff --git a/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/support/BlockingDatabaseWrapper.java b/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/support/BlockingDatabaseWrapper.java index 4d3f57f..e7e5bb6 100644 --- a/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/support/BlockingDatabaseWrapper.java +++ b/embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/support/BlockingDatabaseWrapper.java @@ -120,6 +120,11 @@ public void close() { delegate.close(); } + @Override + public void shutdown() { + delegate.shutdown(); + } + protected static class BlockingConnectionWrapper implements Connection { private final Connection delegate;