Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization and the HTTPSession object

 
Paul Yule
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The pojo needs to be serialized so that it can be replicated across the cluster in different JVMs.
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael Angstadt, I'm thinking, how can a object(attribute) conveyed to another JVM without using serialization?
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic