diff --git a/README.md b/README.md index 3da0f48..3586387 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,10 @@ conn.close(); postgres.stop(); ``` +Note that EmbeddedPostgres implements [java.lang.AutoCloseable](https://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html), +which means that you can use it with a [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) +statement (in Java >= 7) to have it automatically stopped. + ### How to avoid archive extraction on every run You can specify the cached artifact store to avoid archives downloading and extraction (in case if a directory remains on every run). diff --git a/src/main/java/ru/yandex/qatools/embed/postgresql/EmbeddedPostgres.java b/src/main/java/ru/yandex/qatools/embed/postgresql/EmbeddedPostgres.java index cee57ad..6c36efa 100644 --- a/src/main/java/ru/yandex/qatools/embed/postgresql/EmbeddedPostgres.java +++ b/src/main/java/ru/yandex/qatools/embed/postgresql/EmbeddedPostgres.java @@ -26,7 +26,7 @@ /** * Helper class simplifying the start up configuration for embedded postgres */ -public class EmbeddedPostgres { +public class EmbeddedPostgres implements AutoCloseable { public static final String DEFAULT_USER = "postgres";//NOSONAR public static final String DEFAULT_PASSWORD = "postgres";//NOSONAR public static final String DEFAULT_DB_NAME = "postgres";//NOSONAR @@ -215,4 +215,9 @@ private String formatConnUrl(PostgresConfig config) { public void stop() { getProcess().orElseThrow(() -> new IllegalStateException("Cannot stop not started instance!")).stop(); } + + @Override + public void close() { + this.stop(); + } }