Why is sessionDestroyed() called only on invalidate or timeout, but not called when the server is terminating? How I can make some operations on each session when server is terminating?
Why is sessionDestroyed() called only on invalidate or timeout, but not called when the
Share
Regarding your first question:
Here is an explanation of why
sessionDestroyed()is not getting invoked when shutting down in Tomcat (at least as of Tomcat 6.0.33), from a post on Tomcat: http://comments.gmane.org/gmane.comp.jakarta.tomcat.user/215644This is not my post, and I am attributing credit to Violeta, the author of the post.
The post provides a way to patch
StandardManager.java.If you did not want to modify that class (I personally avoid modifying classes that belong to the Application Server whenever possible), there are other approaches you could take.
Regarding your second question:
Why do you want to do operations on each session when the server is terminating? The Servlet specification provides methods for executing code when the server is being shutdown. However, they do not provide a means by which you can perform operations on each active Session (probably by design).
As this earlier answer mentions, How to access HTTP sessions in Java, Session management should be handled by the Servlet Container, and you may reconsider your current approach to your application.
For general handling cleanup when the server is being shutdown, you have
ServletContextListener.contextDestroyedandServlet.destroyThe
ServletContextListenerinterface provides acontextDestroyedlifecycle methodThe Servlet interface provides a destroy method, which is used to release any resources or handle any cleanup when the server is being shutdown.