This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.?
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. HTH Damien
Hi, 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 java.io.Serializable" 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. Dani