Persisting objects in servlet session and java.io.NotSerializableException

 SEVERE: IOException while loading persisted sessions:
         java.io.WriteAbortedException:
         writing aborted; java.io.NotSerializableException:

      

This means that this object cannot be saved to the hard disk.

Does this mean that storing session objects that do not implement "Serializable" is not safe?

I have not heard that there are restrictions on storing non-serializable objects in a Session object.

It just means Tomcat will always keep them in memory, right?

+2


a source to share


3 answers


Does this mean that it is unsafe to save session objects that do not implement "Serializable"?

That's for sure, yes.

However, many servlet containers will let you handle it if they don't really need serialization.



For example, Tomcat doesn't care if session attributes are serializable or not unless you have enabled session replication. It must be able to serialize attributes in order to replicate them to other servers in the cluster.

In your case, it appears that the container is trying to persist the session data to disk, which would also require serialization.

+8


a source


The Servlet specification defines the rules for allocating sessions (the 2.5 spec does not actually describe how to migrate a session to anything other than distributed, but the semantics should be the same). In such cases, it is usually easier to implement Serializable

. You can also use the interface HttpSessionActivationListener

if you want to be notified of these events.



+2


a source


Serialization is performed not only when objects are saved to the hard drive, but also when transferred to another node in a distributed environment. While the Servlet specification does not allow objects to be serialized, this is not too bad.

+1


a source







All Articles