wood burning stoves 2.0*
The moose likes Servlets and the fly likes Serialization and the HTTPSession object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Serialization and the HTTPSession object" Watch "Serialization and the HTTPSession object" New topic
Author

Serialization and the HTTPSession object

Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
I am currently maintaining an App that has 1 complex pojo that gets pushed to the Session. The app has been working fine until we recently moved it to a clustered environment. Now we are seeing odd behavior with data being missing on certain pages that seems mostly sporadic. I assume this means it is happening when I am adding the object on one server and then another server handles the next request which has trouble getting the object off the session. From what I've been able to read around the interwebs is that the complex object that was being pushed to the session object needs to be serializable. I don't quite understand why. If it was reading and writing to the session before why does it have trouble getting when adding another server to the mix?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

The pojo needs to be serialized so that it can be replicated across the cluster in different JVMs.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Yes, the rule is that if you are in a clustered environment with multiple JVMs, then any objects you add to the session scope must implement Serializable. Otherwise, they will not migrate to the other JVM when the HttpSession object migrates.

Side note: Even though the classes implement Serializable, this doesn't mean that the Container will use Java serialization to serialize the objects. So, if you have any custom serialization code in your attribute classes, it may be ignored.


SCJP 6 || SCWCD 5
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Paul Yule wrote:I don't quite understand why. If it was reading and writing to the session before why does it have trouble getting when adding another server to the mix?


For a session to be migrated between distributed JVM's, any attributes attached to it must implement the java.io.Serializable interface. And, I couldn't understand your last words. You just mean that, those object were able to attach and remove to/from the session before, but not now?

And Michael Angstadt, I couldn't understand the last lines, could you please elaborate?

Even though the classes implement Serializable, this doesn't mean that the Container will use Java serialization to serialize the objects. So, if you have any custom serialization code in your attribute classes, it may be ignored.


If it don't use serialization to migrate the attributes from one JVM to another, how will the migration happen?

Thanks!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Abimaran Kugathasan wrote:If it don't use serialization to migrate the attributes from one JVM to another, how will the migration happen?


The specs say that it's up to the Container to decide how to migrate the attributes. It can use Java serialization if it wants to, but it doesn't have to. But even if the Container does not use Java serialization, your session attribute classes must still implement Serializable. The Container will ignore any session attributes that are not Serializable during the migration process.

As a programmer, you must worry about two things: (1) Always make your session attribute classes Serializable and (2) do not include any custom serialization code in your session attribute classes (such as creating a "writeObject" method).
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Michael Angstadt, I'm thinking, how can a object(attribute) conveyed to another JVM without using serialization?
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Abimaran Kugathasan wrote:Thanks Michael Angstadt, I'm thinking, how can a object(attribute) conveyed to another JVM without using serialization?


No problem Abimaran. I would imagine that there are any number of ways. Maybe the Container could convert the objects into some sort of XML representation and send that over the wire. Or maybe it could convert them into a CSV format. The point is that the specs give the Container the option of using its own serialization mechanism if it wants to.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Michael Angstadt, That's it! Then the container uses other method which you pointed, then what is the need of serialization of the attributes?

Thanks!
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Abimaran Kugathasan wrote:Then the container uses other method which you pointed, then what is the need of serialization of the attributes?


I think that it helps to keep your web application Container-neutral. For example, what if you decided to switch from one Container that does not use Java serialization, to a different Container that does use it? You would not have to edit any of your code to account for this difference.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Then the container uses other method which you pointed, then what is the need of serialization of the attributes?

Serializable is a marker interface. In this case it marks objects/classes that are to be migrated with the session to another JVM. As Michael said before, instances of classes that are not Serializable do not get migrated. That also explains the behavior Paul observed: that some data gets migrated, but not all of it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization and the HTTPSession object