Tuesday, March 20, 2007

How-to turn off tomcat session serialization

Keywords:
tomcat session serialization NotSerializableException "Cannot serialize session attribute" Manager

Problem:
On restarting tomcat there's a stacktrace (below) about an attribute in the session not being serializable. It doesn't seem to effect the application starting up and the application works fine, but these messages at startup are annoying.

2007-03-20 10:50:57,890 INFO [org.apache.catalina.session.ManagerBase@898] - 
java.io.NotSerializableException: com.example.package.MyBeanClassName
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1054)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
        at java.util.ArrayList.writeObject(ArrayList.java:531)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


If you want to put (transient) non-serializable objects in the session, how do you stop tomcat from serializing them at shutdown/startup?

Solution:
This was hard to find, but you need to configure a Manager in the context of the application. This should be put in either:
  • the YourApp.war:/META-INF/context.xml

  • the TOMCAT_HOME/conf/Catalina/localhost/YourApp.xml file

  • OR the default context ...

<!-- Turn off session serialization -->
    <Manager className="org.apache.catalina.session.PersistentManager"
      debug="0" distributable="false" saveOnRestart="false">
        <Store className="org.apache.catalina.session.FileStore"/>
    </Manager>


The tomcat documentation for the Manager element will give a bit more information, but it doesn't show the className for the file-store Store element, which is crucial if you're using the PersistentManager class - even if you're configuring it to do nothing!

You'll know it's working when there are no longer any files called "SESSIONS.ser" in the TOMCAT_HOME/work/Catalina/localhost/YourApp folder.

1 comment:

adeon said...

This can be also done by adding this:

< Manager pathname="" / >