Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Commit

Permalink
Make EmbeddedPostgres implements AutoCloseable (#131)
Browse files Browse the repository at this point in the history
This allows to use it in a try-with-resources statement, in order to
have it automatically closed/stopped.
  • Loading branch information
rm3l authored and smecsia committed Apr 26, 2018
1 parent 3c208e1 commit ee7ef49
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}

0 comments on commit ee7ef49

Please sign in to comment.