Calling System.exit() in Servlet's destroy() method

后端 未结 4 547
迷失自我
迷失自我 2020-12-15 14:01

This is a follow up to my earlier question.

Tomcat 5.0.28 had a bug where the Servlet\'s destroy() method was not being invoked by the container on a shutdown. This

4条回答
  •  天命终不由人
    2020-12-15 14:31

    Calling System.exit() inside a Servlet's destroy() method to forcefully kill any non-daemon threads is a good idea?

    It is absolutely not a good idea - it is a horrible idea. The destroy() method is called when the servlet is taken out of service, which can happen for any number of reasons: the servlet/webapp has been stopped, the webapp is being undeployed, the webapp is being restarted etc.

    System.exit() shuts down the entire JVM! Why would you want to forcibly shutdown the entire server simply because one servlet is being unloaded?

    Why does Tomcat 5.0.30 and (later versions including Tomcat 6.x.x) fail to shutdown properly if there's a System.exit() in the destroy() method of the Servlet.

    Probably to prevent such dangerous behavior like this.

    You shouldn't write code that assumes that your code/application is the only thing running on the server.

提交回复
热议问题