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.Logger is 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.
Other Serialization and troubleshooting articles from Javarevisited
Blog
6 comments :
Hi,
I am also facing same issue only in Cluster environment.
public class ViewAction implements Serializable{
private static Logger LOGGER = Logger.getLogger(ViewAction.class);
....
}
Error:
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.MarshalException: CORBA BAD_PARAM 0x4f4d0006 Maybe; nested exception is:
java.io.NotSerializableException: org.apache.log4j.Logger is not serializable
at com.ibm.CORBA.iiop.UtilDelegateImpl.wrapException(UtilDelegateImpl.java:749)
at javax.rmi.CORBA.Util.wrapException(Util.java:296)
at com.cib.infra.txn._TxnProcessorRemote_Stub.handleRequest(Unknown Source)
at com.ws.framework.PortletAction.connect(PortletAction.java:461)
at com.ws.framework.PortletAction.executeHostRequest(PortletAction.java:435)
at com.ws.view.action.ViewAction.getExportData(ViewAction.java:294)
at com.ws.services.exportdata.ExportServiceServlet.doPost(ExportServiceServlet.java:141)
at com.polaris.iportal.ws.services.exportdata.ExportServiceServlet.doGet(ExportServiceServlet.java:335)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:718)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
for which class, it it for this class then you already made Logger instance static. I think you should check ExportServiceServlet.java
in my ExportServiceServlet.java it declared like below one.
public class ExportServiceServlet extends HttpServlet
{
private static Logger logger = Logger.getLogger(com/ws/services/exportdata/ExportServiceServlet);
...........
}
That's also static. Can you let me know when does this error occur, I mean which object are you trying to store in Session? Another way is look for a logger instance which is not static.
Thanks for Suggestion...
In PortletAction class logger instance is not static. Will this cause issue?
Also Is that required to declare it like "private static final" or "private static" is enough...
public abstract class PortletAction extends IOrbiAction
{
public PortletAction()
{
logger = Logger.getLogger(com/ws/framework/PortletAction);
}
............
private Logger logger;
}
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.
Post a Comment