zonkyio / embedded-postgres

Java embedded PostgreSQL component for testing
Apache License 2.0
344 stars 43 forks source link

Option to disable shutdown hook #64

Closed dmoebius closed 2 years ago

dmoebius commented 3 years ago

EmbeddedPostgres always sets its own shutdown hook in method startPostmaster():

Runtime.getRuntime().addShutdownHook(newCloserThread());

This gives us some problems in some test scenarios, where we don't have control of the order of shutdown hooks. Could you please add an option to disable the shutdown hook, e.g.

if (!System.getProperty("ot.epg.no-showdownhook", "false").equals("true")) {
    Runtime.getRuntime().addShutdownHook(newCloserThread());
}

Thanks.

tomix26 commented 3 years ago

Could you please describe your test scenarios in more detail? Does it mean that you access a test database from a shutdown hook? Because in normal use you should only access the test database within test methods, or possibly also within before or after test callbacks. You should definitely not rely on the execution order of shutdown hooks.

dmoebius commented 3 years ago

We are using HikariCP as database connection pool. Hikari has it's own shutdown hook. Since we cannot control the order of shutdown hooks, very often the EmbeddedPostgres shuts down first, which causes Hikari to throw many "not available" exceptions during shutdown, This is annoying and confuses our developers and testers.

We are not only using EmbeddedPostgres in JUnit-Tests, but also during development when running the application locally and interactively. The application is stopped using Ctrl-C on a terminal. We don't have a clean way to stop Hikari before stopping the rest of the application.

tomix26 commented 2 years ago

Sorry for the late response. However, this library is intended for testing-only purposes. So it should be used only for running unit or integration tests. Using it for running the application is neither intended nor supported, even during development. Thanks for your understanding.