Breaking News
Home / ServerSide / Cannot Serialize the Session

Cannot Serialize the Session

Serialize Web Session At times in the application server logs we see this exception with the following details.

<Could not deserialize session data. java.io.NotSerializableException: <package>.<className>
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1059)<!>
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
        at java.util.ArrayList.writeObject(ArrayList.java:531)
        at sun.reflect.GeneratedMethodAccessor149.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:324)
        at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:809)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1301)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1252)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1057)
        at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1337)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1309)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1252)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1057)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:278)

Basically this happens when the JVM is trying to serialize the object to the disk, one or more of the objects cannot be serialized resulting in this exception. Normally this kind of exceptions is common when objects are stored in Web Session  cannot be serialized to the disk.

In order to solve this we need to implement Serializable Interface all thes objects classes stored in Session (along with child objects), so that they can be serialized, else avoid putting these objects in Session. By simply implementing the Serializable interface instructs the JVM to serialize the object using the default inbuilt serialization algorithm. However one can implement custom serialization algorithms for each class as required. For most part the default implementation is enough.

Example.

public class Customer implements  java.io.Serializable
{
    members; // make sure all of these also implement Serializable as well
    getters()
    setter();
}


[pb_builder]

Check Also

How does a typical Log4j configuration looks like?

Answer Log4J is a very popular and commonly used logging implementation that is used in …

Advertisment ad adsense adlogger