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.