-
Notifications
You must be signed in to change notification settings - Fork 41
Description
I have a situation I am using Zonky as an embedded postgres database outside of the unit- / datajdbc- / datajpa tests. We are using the spring-boot-maven-plugin to start our application with an embedded postgres through a spring bean (see below) in phase pre-integration-test. We don´t want to replace the datasource beans because we want the application to be as close as to production as it can be. When the application is started the maven-failsafe-plugin runs the integration-test against the started application. This works but when the application shuts down the embedded postgres is one of the earliest things to close while other spring beans (i.a. datasources) that rely on the database are not destroyed yet. The application can´t shutdown gracefully this way. This causes a lot of stacktraces and the time to stop the application is significant longer.
The problem is that EmbeddedPostgres always registers a shutdownhook on the jvm meaning that it will stop independently from the spring context and very early in the process.
Runtime.getRuntime().addShutdownHook(newCloserThread());
As a workaround I copied the entire EmbeddedPostgres class and removed the above line of code that registers the shutdownhook. Now the application can shutdown gracefully because Spring is able to destroy the bean in the right order.
I think it would be nice to have the possibility to disable the registering of the shutdown hook (through a boolean in the builder named springShutdown?). If you agree with me I am happy to make a PR to make this possible.
For reference:
I am creating my own EmbeddedPostgres bean as follow.:
@Bean
@ConditionalOnProperty(value = "spring.datasource.start-server", havingValue = "true", matchIfMissing = true)
public EmbeddedPostgres embeddedPostgres(DataSourceProperties dataSourceProperties
, @Value("${embedded.postgres.working-directory:./target/pg-embedded}") String workingDirectory
, @Value("${embedded.postgres.clean-data-directory:true}") boolean cleanDataDirectory
) throws IOException, URISyntaxException {
return EmbeddedPostgres.builder()
.setOverrideWorkingDirectory(new File(workingDirectory))
.setPort(5432)
.setDataDirectory(workingDirectory + "/data")
.setCleanDataDirectory(cleanDataDirectory)
.setServerConfig("max_prepared_transactions", "300")
.start();
}