18 Mayıs 2016 Çarşamba

Caused by: java.io.NotSerializableException: bean.Example

NotSerializableException is common exception type on run-time of Java web applications when Tomcat server restarts. This is due to Tomcat's default behavior of serializing all the sessions and after restart trying to deserialize them. This is the persistance feature of Tomcat server.

Caused by: java.io.NotSerializableException: bean.Example

1) In the exception above, "bean.example" is fully qualified name of class that does not implement the Serializableinterface while it is been expected by the code behind. To fix it:

package bean;

import java.io.Serializable;

public class Example implements Serializable {
   private static final long serialVersionUID = 1L;
  private DeviceManager deviceManager = new DeviceManager();
// ... }
There is another important thing that Tomcat persists sessions and all objects in each session. Hence, if there is a variable that is used to keep instance of another classses, they also should be implemented with the Serializableinterface.

2) Sometimes, finding the class that throws the exception and making it implement theSerializableinterface may not be feasible when the class belongs to a third-party library. Objects that are created from this kind of class should not be serialized, but these objects can be declared as transient. Once a field of a class is declared as transient, then, it is ignored by the serializable runtime.


package bean;

import java.io.Serializable;
import com.google.gson.*;
public class Example implements Serializable { private static final long serialVersionUID = 1L;
  private transient Object deviceInformation = null;
// ... }
3) Another solution to get rid of NotSerializableException may be disabling session persistence in Tomcat server. To disable it, find tomcat_dir/conf/context.xml and find a place where it says:

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

* This solution is valid for both Tomcat 6 and Tomcat 7.
References:
  1. http://stackoverflow.com/a/11692208/1194553
  2. https://mihail.stoynov.com/2011/04/11/disable-session-persistence-in-tomcat/
  3. https://examples.javacodegeeks.com/java-basics/exceptions/java-io-notserializableexception-how-to-solve-not-serializable-exception/
  4. http://stackoverflow.com/a/1904705/1194553

Hiç yorum yok:

Yorum Gönder