Monday, December 25, 2017

java.io.NotSerializableException:
org.apache.log4j.Logger error says that instance of org.apache.lo4j.Logger is not
Serializable. This error comes when we use log4j for logging
in Java and create Logger in a Serializable class e.g. any domain class or
POJO which we want to store in HttpSession or want to
serialize it. As we know from 10
Java Serialization interview question that, if you have a non serializable
class as member in a Serializable class, it will throw java.io.NotSerializableException Exception.

Look at the below code :

public class Customer implements Serializable{

private Logger logger =Logger.getLogger(Customer.class)

......

}

If instance of Customer will be stored in HttpSession or
Serialized externally it will throw "java.io.NotSerializableException:
org.apache.log4j.Logger" because here logger instance is neither static
or transient
and it doesn't implement Serializable
or Externalzable interface.

How to solve
java.io.NotSerializableException: org.apache.log4j.Logger

Solving java.io.NotSerializableException:
org.apache.log4j.Loggeris simple,
you have to prevent logger instance from default serializabtion process, either
make it transient
or static. Making it static final is preferred option due to many reason
because if you make it transient than after deserialization logger instance
will be null and any logger.debug() call will result in NullPointerException
in Java because neither constructor
not instance initializer block is called during deserialization. By making it static
and final you ensure that its thread-safe
and all instance of Customer class can share same logger
instance, By the way this error is also one of the reason Why Logger should
be declared static and final
in Java program. Just make following code change to fix java.io.NotSerializableException:
org.apache.log4j.Logger in Java.

public class Customer implements Serializable{

private static final Logger logger =Logger.getLogger(Customer.class)

......

}

That's all on how to fix java.io.NotSerializableException:
org.apache.log4j.Logger in Java. We have seen what cause
java.io.NotSerializableException: org.apache.log4j.Logger, it's because Logger
class is not Serializable but we also learned that there is no point
serializing Logger instance and better to make
Logger static and final.

Yes, this will cause issue, if you try to put any object, which directly or indirectly has reference of this class. Make it private static. Regarding your question, its always good to made static variable final, to avoid accidental change, because it's shared between instances. to avoid java.io.NotSerializableException, its enough to making it static.

Hi I'm encountering similar intermittent error java.io.NotSerializableException: oracle.jdbc.driver.T4CConnection. Could you suggest what approach could be taken for the same.Could the above approach of making private static would help in this case

@Javin Paul: Hi, when i try to make a remote ejb call to the bean , it seems to throw java.io.NotSerializableException: oracle.jdbc.driver.T4CConnection. tried making connection object private static. but did not help. The environment is Jboss eap 7(wildfly)