Originally posted by Alok Pota:
A good MVC design mandates that the M (bean), V (JSP) and C (Servlet) be as seperate as possible. A suggestion that our team-lead has asked us to live by is to not have any SQL/DB related objects in the V. The V is strictly for V (whatever that means). This strict partioning although elegant and very *OO* comes with a terrible cost. We need to transfer the data in the M to V and to do that we need an object (HashMap, List, TernaryTree, whatever).
What should the scope of this object be?
A request scope would mean creating and *nulling* this object everytime I do a "V". Creating many short lived objects, something folks in the performance forum would possibly chide you about.
A session scope makes the objects *not so short lived*. I will be holding up the object's in memory for the time the user is logged in.
A page scope (I think) translates into defining instance variables on the servlet.
An application scope is too broad and I am not sure how it would pan out in a web cluster?. Wouldn't the application/servlet contexts be different for the different web servers?
Of the different scopes, session scope is I think the only one where a container has callback methods it calls on the bound object. (valueBound,valueUnBound).
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Originally posted by Alok Pota:
The particular scenario I am talking about is an HTML tree. When I click a node I signal the tree to expand/collapse and the entire tree is redisplayed with all the expanded/collapsed nodes.
The nature of the problem is such that the the number of nodes for my tree is not trivial (~200 nodes per level, 3 levels deep). Putting the tree model in the request scope would not be optimal. (Any opinions?).
I have come out with a compromise, such that I have stuff in the session scope and I use a timer to clear the session. This is ugly but atleast I have control over the *nullying* process. I wonder if the Runtime.getRuntime.gc() even gets called when a request scoped bean goes out of scope!.
Maybe I am too worried about optimization at this stage when I should be concerned about design. But I can see this thing being a performance bottleneck once it goes production.
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Originally posted by Alok Pota:
Interestingly, the model you described is exactly what I have
The problem is that the data in Model A is huge and storing it in any global context (JSP application/servletcontext/JNDI) takes up and hangs on to some chunk of memory for the lifeof the server.
Model B carries lightweight data which gets created per user. Model B data *hopefully* gets GC'd when the user session is invalidated, through an attempt to null Model B data in the valueUnbound(HttpSessionBindingEvent method)
From what it seems that Runtime.getRuntime().gc() is a request and not a command and so there is no guarantee that all that replicated Model A & Model B data gets GC'd.
Whats worse is that users who close their browsers and log back in multiple times end up having replicated copies not cleared until the server kicks off their session
[...] *OutOfMemoryError* [...]
Does that mean the old fear of creating too many short lived objects is uncalled for if you are using HotSpot?
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Consider Paul's rocket mass heater. |