Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it necessary to serialize any class before adding object of that class to session  RSS feed

 
mahesh shastri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys ,
I got an exception when I was adding an object which is a DTO to session for further reference. I did that many times previously
but this time I got an Exception called java.lang.IllegalArgumentException: setAttribute: Non-serializable . In my project which is
role based I dont get this exception if I login through other role. Both roles are using same method . I am getting exception for one
role and the other role wont. And when I serialized that class I dont get any exception program gets executed.
My Question is
1)Why I am getting exception for one role and not for other when the class is not serialized ?

2)Is it necessary to serialize your class whenever you want to add object to session ?

Thanks and Regards

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. Perhaps that object has a non-serializable member? You left out when and where the exception is generated so I am just guessing.

2. No, don't be silly, it is up to the servlet container to serialize sessions out to disk when it needs to.

Bill

 
mahesh shastri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Perhaps that object has a non-serializable member? You left out when and where the exception is generated so I am just guessing.

2. No, don't be silly, it is up to the servlet container to serialize sessions out to disk when it needs to.

Bill

Thanks for the reply William.
As you said that object may have non-serializable member . If so, that is applicable for every role but it doesn't happen.
Anyways I implemented serializable for the object which was generating exception.

Thanks
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to review - you may store a reference to a non-serializable object in a session but you are taking a chance that it may not be there next request because the container may decide to serialize the session to disk.

If you need to keep a reference to a non-serializable object during a request/response cycle there are plenty of ways to do it that don't involve a session.

Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!