Issue
This Content is from Stack Overflow. Question asked by Ilia
I have Spring boot application, that goes to database using HikariCP (several threads use it).
The thing is, when application gets stop (SIGTERM) signal, it must go to database and change some data, after that it can be closed (“gracefull shutdown”). This logic is written in myShutdownHook Runtime.getRuntime().addShutdownHook(myShutdownHook)
. So the app needs to use connection pool for that. But problem is that Hikari pool closes right after getting stop signal:
18:26:44,796 INFO [SpringApplicationShutdownHook] com.zaxxer.hikari.HikariDataSource: HikariPool-1 - Shutdown initiated...
18:26:44,798 INFO [SpringApplicationShutdownHook] com.zaxxer.hikari.HikariDataSource: HikariPool-1 - Shutdown completed.
And context is closing before my logic is completed.
How can I go to database and change data there after getting shutdown signal, but before connection pool and application context closed?
Solution
The way u register ur shutdownhook is at JVM level and does not block Spring’s shutdownhooks (i.e. springs shutdown process is destroying it’s own context).
Try using one of springs shutdown hooks, they will wait.
Be wary though that ur shuwtdownlogic does not take too long…
This Question was asked in StackOverflow by Ilia and Answered by tickrate It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.