Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way tol load and access application scoped collections?

 
David Heffington
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi-
I need to load some collections with values used to populate pull-down selection menus throughout the web app. I am sure the most efficient approach is to make these collections accessible from application scope, thus eliminating the need to load the collections for each sessions (or worse, any request requiring the collection). I know this can be done with static variables in a class (and just test to see if the variables are loaded or not whenever they are accessed), but I would like use a better method for handling this.

ps. I hope this post is in the right place
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are the values constant ? Will they be changed ?

If they won't be changed, then as you said, use statics.

Otherwise, if they are going to be populated from some data-source (database as an example) i suggest the Ajax technology to be used.

Regards,
 
David Heffington
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The values will be constant most of the time. However, the possibility exists that they could change in the database. When they do change in the database, there will need to be a 'refresh' mechanism in place that can be manually invoked in the event it is needed (i.e. for the static var approch mentioned above, there could be an accompanying static var (such as 'static boolean isLoaded') that could be resest to false by some process and then tested by the next request needing the collections, triggering the reload of the variables). Just wondering if there was a better way than the use of static vars.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to fetch these values from the DB every time they are need -- thereby guaranteeing that they are always up to date -- you'd just handle that (with proper delegation to the Mode, of course) in the page controller for the JSP.

However, it sounds as if you;d rather cache the values at app startup in order to avoid the overhead of continually grabbing the values from the DB.

In that case, I'd use a context listener to get the values from the DB at app startup and store them in the application context (servletContext) so that they can be easily obtained while the app is running.

The interesting question then is: what do you do if the DB changes?
 
David Heffington
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would much rather use the listener approach. But, am I then stuck with restarting the application every time a refresh is needed? (even though it would be rare)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps not.

If an update is rare, what I'd probably do is have an administrative command that I could send to the web app that would reload the values from the DB.
[ April 17, 2008: Message edited by: Bear Bibeault ]
 
David Heffington
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your response, I think you are referring to a separate utility to reload the existing collection objects on command. I guess I could include a button (just add a button ) that only adminitrators could see with something like 'reload cache'.

That should work.

Thanks!!!
 
David Heffington
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait a minute. What if multiple srvers are being used for load balancing. Would the 'reload' button mentioned above only reset the cache on the server the user happens to be on?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure that's possible. In fact, some of my apps have an entire admin console sub-system.

But it's also possible for really simple stuff to just have a URL you can hit that executes an "admin" servlet to perform the administrative action.
 
David Heffington
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's true. The utility to perform the updates could point to the specific URLs for each server in the cluster. I guess this URL list could be maintained as init-params in the DD as well.

So, would you write a separate servlet for admin functions, point to the explicit URL for each admin servlet, and do the refresh using something like this: �getServletContext().setAttribute( �list�, refreshedCacheObject )� ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic