Thursday, December 12, 2013

I would like to know if the object calling the session.setAttribute or session.getAttribute should be also needed to implement the "java.io.Serializable" in order to support the session replication?
for example
the class User is implemented the Serializable interface. But if the class SomeUtilityClass that calling "session.setAttribute" is required to implement the Serializable interface.

/* Does it also need to implement the Serializable here? */
public class SomeUtilityClass{
    public void test(HttpServletRequest request){
        request.getSession().setAttribute("user", new User());
    }
}

public class User implements Serializable{
        private static final long serialVersionUID = 12345432534456654664L;
        //skip the getter & setter...
}
And is there any problems if the variable serialVersionUID has been omitted for the session replication?
thanks in advance.
share|improve this question
add comment

2 Answers

Only objects that go into the session need to be serializable (i.e. Implement that Serializableinterface)
That means any object that is set as an attribute, plus any objects that are contained inside those objects.
So if you have a class
public class User implements Serializable {
      private Department department;

// ...
}
then your Department class also needs to implement Serializable
Classes that simple make calls on the session (include calls to setAttribute) do not need to implement Serializable (that includes SomeUtilityClass)
The reason is that anything that is inside the session has to be sent to the other WebLogic node (that is probably running on a different server). That means that it needs to send your User class over the network, so it needs to be able to turn your User class into a series of bytes, that it can send, and then turn it back into a real object again on the other side.
When you implement the Serializable interface you're telling Java that you've built your class in a way that it's OK for it to turn in into those bytes, (and back again). If you don't implementSerializable then Java doesn't know whether it OK to do that, so it doesn't. Note: The code for the class isn't being sent around, just the values of the fields inside it. The code is already on the other node, because it's part of your application that is already deployed to all nodes already.
Your utility class, doesn't need to go over the network. You haven't put an object of that class into the session, so WebLogic doesn't need to turn it into bytes (and back again_) as part of session replication.
The only things that need to be Serializable are the objects that get put into the sessioneither directly (like your User) or indirectly (like the Department example).
Objects that simply make calls on the session do not need to be Serializable.
share|improve this answer
 
Very details and useful info. Thanks. –  Cube Feb 4 '11 at 12:26
add comment
If your utility class does not have state to be replicated across server instances then there is no need for it to be serializable. As for the serialVersionUID, is a whole new discussion on its own.
share|improve this answer
 
thanks for the link first. Could you mind explain more about the "state to be replicated..."? Actually, i have some utiltiy classes and struts' actions classes which contain some functions using the session.setAttribute/getAttribute. I am quite not sure whether they need to implement the interface or not. thx –  Cube Feb 4 '11 at 5:32
add comment

No comments: