You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Karr, David" <Da...@wamu.net> on 2003/05/27 19:09:45 UTC

Proper way to deal with serialization of session attribute with Logger attribute

I'm using JDK 1.4.1, and Tomcat 4.1.24.

I was seeing a strange situation where some cactus/ant tests of mine
would succeed on one run, and then fail on the next, and alternate in
that pattern continuously.

After looking carefully at the Tomcat logs, I discovered that a class
that is instantiated and then put into the session has a Log4J Logger
object.  Tomcat tries to persist that session, but it fails because
Logger is not serializable.

If I want to allow objects of this class to be serialized and
deserialized, how do I deal with the contained Logger object?

The class presently defines it's "logger" instance variable like this:

	protected final Logger logger =
Logger.getLogger(this.getClass());

Do I instead have to make this "transient" and change all the references
to the "logger" instance variable to call an accessor which tries to
initialize the value first if it's null?  Or perhaps do I have to add
"writeObject()" and "readObject()" methods where the "writeObject()"
method specifically writes all the fields but this one, and the
"readObject()" method reads all those fields, and then manually sets the
"logger" value?



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Proper way to deal with serialization of session attribute with Logger attribute

Posted by Tom Oinn <tm...@ebi.ac.uk>.
Make the logger static? Effectively the same as you're creating it from 
the class name anyway, and avoids attempts to serialize it as it's not 
attached to an object of any kind.

Tom

Karr, David wrote:
> I'm using JDK 1.4.1, and Tomcat 4.1.24.
> 
> I was seeing a strange situation where some cactus/ant tests of mine
> would succeed on one run, and then fail on the next, and alternate in
> that pattern continuously.
> 
> After looking carefully at the Tomcat logs, I discovered that a class
> that is instantiated and then put into the session has a Log4J Logger
> object.  Tomcat tries to persist that session, but it fails because
> Logger is not serializable.
> 
> If I want to allow objects of this class to be serialized and
> deserialized, how do I deal with the contained Logger object?
> 
> The class presently defines it's "logger" instance variable like this:
> 
> 	protected final Logger logger =
> Logger.getLogger(this.getClass());
> 
> Do I instead have to make this "transient" and change all the references
> to the "logger" instance variable to call an accessor which tries to
> initialize the value first if it's null?  Or perhaps do I have to add
> "writeObject()" and "readObject()" methods where the "writeObject()"
> method specifically writes all the fields but this one, and the
> "readObject()" method reads all those fields, and then manually sets the
> "logger" value?
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org