Search code examples
sessiontomcatdeploymenttomcat8

Re-deploy of a Tomcat application causes the active sessions to be dropped


I have a tomcat application which I deploy by placing the new my-project.war file into the /var/lib/tomcat8/webapps/ directory on my server (via scp).

Every time I do this, the new copy is live a few seconds later. The only problem is that it drops all the sessions.

All the documentation I can find tells me that Tomcat should restore the sessions by default. This does seem to be the case when I restart the Tomcat service, but not when I redeploy...

Can anyone tell me whats going on here? I don't fully understand why it works to deploy this way in the first place.


Solution

  • The default session manager - StandardManager saves the sessions in <tomcat-home>/work/<app-name>/SESSIONS.ser. When you stop tomcat, the sessions are serialized into that file. When you start it - they are loaded from it.

    But when you re-deploy your applications two things happen:

    1. Tomcat un-deploys the old app and deletes the <tomcat-home>/work/<app-name>/ folder, thus removes the saved sessions

    2. Tomcat deploys the new app and creates a new folder

    Or in two words - tomcat deletes the saved sessions on un-deploy. I guess this is because:

    • the re-deploy is implemented as undeploy -> deploy. I.e. the 'undeploy' step does not know if a new version of the app will be deployed, so it makes sure to properly clean up after itself.

    • it's better from security perspective