Disable session persistence in Tomcat

This a pretty common issue when running java web applications on Tomcat. One puts an object into session and after a restart one gets:

Caused by: java.io.NotSerializableException: java.lang.Object

It may not always be java.lang.Object, any class not implementing java.io.Serializable could be in this error.

This is due to Tomcat's default behavior of serializing all the sessions and after restart trying to deserialize them. (A good question is how does tomcat serialize them in the first place, but no time to research that).

The solution is pretty simple, find tomcat_dir/conf/context.xml and find a place where it says:

<!-- Uncomment this to disable session persistence across Tomcat restarts -->

This works for both Tomcat 6 and Tomcat 7. Tomcat guys did the effort to prepare everything so it's easy for us.

This solution deserves a blog post because every once in a while I get bugged by this problem and have forgotten the solution. Now I know where to search for one.

8 thoughts on “Disable session persistence in Tomcat”

  1. i never knew this. i am always curious why i always have serialization error when restarting my tomcat server. i always think it is a bug in my application

  2. Thanks very much. I was handling with this error for days and also thought it was a bug in my application.

  3. Or, if you want your server to persist sessions on restart, all your objects, that are added to session attributes must implement Serializable.

    1. ..on a million objects (imagine having a cart with products and metadata objects and stuff).

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.

This site uses Akismet to reduce spam. Learn how your comment data is processed.