Can an Application Scope Variable be clustered?

John P Jensen

[ For example I want to load some settings when the application starts and i keep that object in the application scope so that any part of the application can use that information. Now if the environment is clusterd will this Object available across the clustered application?]


No, an object saved with 'application scope' (Saved in the ServletContext) is not truly global.

As per Servlet 2.2 spec:

4.3.1 Context Attributes in a Distributed Container
Context attributes exist locally to the VM in which they were created and placed. This prevents the ServletContext from being used as a distributed shared memory store. If information needs to be shared between servlets running in a distributed environment, that information should be placed into a session, a database or set in an Enterprise JavaBean.

Oh.. By the way...

If the data is doesnt change often (like a city-zipcode table) there is no problem using application scope.
Just initialize it when your Container starts up!

The fun starts when your servlet begins to update the Object. An update to a Context done by a Servlet in Container A is not propagated to the Context in Container B!

Personally I wouldnt use the the ServletContext as a parking place for global variables - I see it as a place for holding information relevant for the application infrastructure (paths, datasource names etc) - not for the application logic.

I would place the data as a static instance variable on the servlet!

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact