I have a lot of data that needs to be available to all users, at any time (for example, the contents of drop-down menus, which I loaded from an XML file). From all the posts I've seen here, the recommended place to store global data seems to be the servletContext. Most people are setting up a ServletContextListener, and loading this data in the contextInitialized method.Not necessarily. When you deploy your application on a cluster, the contextInitialized() method is going to get fired on each node, so that if your data is read only you'll be making a copy of the same information inside each node's memory space. The bytes are not actually shared, but the net effect is the same ... requests being handled on different nodes see exactly the same data in application scope. This scenario would seem to cover use cases like your caching the contents of dropdown menus.
But according to the servlet spec (http://jcp.org/aboutJava/communityprocess/final/jsr154/index.html), page 32:
"Context attributes are local to the JVM in which they were created. This prevents ServletContext attributes from being a shared memory store in a distributed container. When information needs to be shared between servlets running in a distributed environment, the information should be placed into a session (See ChapterSRV.7, ³Sessions²), stored in a database, or set in an Enterprise JavaBeans TM component."
I'm going to be running in a cluster...does this mean that I can't use the servletContext?
If you need the shared data to be modified, and also ensure that those modifications are propogated, you'll need to use one of the other techniques.
If that's the case, I have two other questions:You can't put anything into a session until it is first created. In general, there will be a session for each user, created automatically the first time that user requests a resource that declares it wants to participate in the session (which JSP pages declare by default, for example).
- When do I first put the data into the session? I don't think I have access to it in the ServletContextListener.
Sessions are not a solution to your "share this data with all users" requirement, because by definition they are specific to individual users.
- How do I prevent it from being replicated for all users?Use a different technique (context attributes are fine for data that doesn't change; otherwise store things in databases or shared files or EJBs or whatever technology you have that is accessible to all nodes in the cluster).
Another question: I may have 50 or so pieces of data. When I store them (in either the session or the servlet context), would you recommend putting them all in as separate attributes, or wrapping them all into one bean and just storing that one bean as an attribute? Or does it not matter?It doesn't matter enough to matter :-) from a performance perspective ... I'd go with what makes life easiest to (a) maintain over the long term, and (b) access using property or EL expressions. The latter issue would encourage having more beans (as opposed to more properties on a single bean, or nested trees of beans) because the accessor expressions will be shorter.
Craig McClanahanThanks for any help. Jacob
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]