File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Session object and clustering Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session object and clustering" Watch "Session object and clustering" New topic

Session object and clustering

Debbie Gumm

Joined: Jul 30, 2003
Posts: 1
Hello. I plan to save an object in a session. For example, session.setAttribute("myObjectName", theObject);
Currently i have one server/web container only. My question is:
1) what if we need to cluster and may run the web application on 2 or more web containers - will i encounter any problem with the session?
2) If there are now two web containers and thus 2 or more jvms - do i need to serialize "theObject"?
3) If i use a primitive or a String instead of "theObjectReference" will this be an easier/better solution - i.e. i don't need to serialize etc.?
damien malone
Ranch Hand

Joined: May 06, 2003
Posts: 35
There is a lot of overhead in having several web servers keeping track of one session, so you could run into performance issues if its a busy site. Last time I was working on something like that, we decided to go for sticky sessions. In that case, there were two web servers, but when a person hit the web site, they were assigned one server, and all requests by that person were serviced by the one machine. A load balancer was used to assign the session to the box that had the least amount of traffic at the time.
Dani Mazzuca
Ranch Hand

Joined: May 21, 2003
Posts: 70
1) Well, no problem, because you appplication server deals with almost all the things. However, your session could migrate from one machine to another, and because of that, all the object in the session MUST implement Serializable.
2) YOU don't need to serialize the object, you shoul turn the object serialuizable. In most cases you should only add "implements" to your class definition. The serialization, however, is done by the container. You also have some listeners that enable you to take some cares when the session is going to migrate from one machine to another (HttpSessionActivation).
3) You cannot add primitives as attributes to the session. But again, in most of the cases you only need to write "implements Serializable" in the objetc in order to let the server serialize it.
I agree. Here's the link:
subject: Session object and clustering
jQuery in Action, 3rd edition