Prevent database shutdown command from Spring Boot

折月煮酒 提交于 2019-12-13 17:24:52

问题


When I run my Spring Boot application (from within Intellij IDEA) and either trigger a stop, restart or automatic redeploy of the application; something is issuing a shutdown command to my HSQLDB.

I run a HSQLDB (v.2.3.4) in Server mode from an external terminal window.

HSQLDB Server log at the moment I restart or stop my Spring Boot application:

[Server@4f023edb]: Initiating shutdown sequence...
[Server@4f023edb]: Shutdown sequence completed in 101 ms.
[Server@4f023edb]: 2017-05-05 21:47:01.878 SHUTDOWN : System.exit() is called next 

This is of course very annoying since I have to go through the hassle of manually bringing up my HSQLDB every time I redeploy the application. How do I prevent this from happening, or an explanation of what is actually going on.

This only seems to happen when running the Spring Boot application from within Intellij IDEA, if I start the Spring Boot application-jar from a terminal window and issue shutdown Ctrl+C, then HSQLDB is not affected.


回答1:


Turns out the reason why I only experience this problem when running from within Intellij IDEA is because spring-boot-devtools (a maven dependency included in my project) is not packaged in the application-jar that I run from a terminal window.

Developer tools are automatically disabled when running a fully packaged application. If your application is launched using java -jar or if it’s started using a special classloader, then it is considered a “production application”. Flagging the dependency as optional is a best practice that prevents devtools from being transitively applied to other modules using your project. Gradle does not support optional dependencies out-of-the-box so you may want to have a look to the propdeps-plugin in the meantime.

spring-boot-devtools is active when running the application from within Intellij IDEA and provides a shutdown hook that will try to gracefully shutdown the database resource (amongst other things).

The shutdown hook can be disabled the following way;

SpringApplication app = new SpringApplication(MyApplication.class);
app.setRegisterShutdownHook(false);  //This disables the shutdown hook
app.run(args);

This solution resolved the problem I had.



来源:https://stackoverflow.com/questions/43812822/prevent-database-shutdown-command-from-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!