File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Better practice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Better practice?" Watch "Better practice?" New topic
Author

Better practice?

Jason Kwok
Ranch Hand

Joined: Mar 31, 2005
Posts: 126
Hi,

I have a simple web application, and it's sole purpose is to display a university department's information. For example, information about instructors, courses being offered, books available, project deadlines, new items, etc... which to this point I've made a servlet handle loading from the database.

This "startup" servlet will read in the database information once a day and save it to the application scope. For example, I read in all of the instructors and their corresponding information and store each instructor individually into a bean made specifically for them... and then store all the instructor beans into an arraylist. This arraylist is then saved to the application and can be retrieved by any page that needs it.

I thought this would be a good idea because the information in the database is seldomly changed, for the most part, and used by multiple pages throughout the entire application. Basically, I thought it would save on processing if the information was held by the container instead of requerying the database with each individual request throughout the day.

Is this a good idea? Or should I just make it read from the database with each individual request for particular information?

Thanks to any suggestions, and apologies in advance if this is the wrong place to ask!!

Jason
peter cooke
Ranch Hand

Joined: Mar 16, 2004
Posts: 314
Darn it I cannot remember the patterns.

Write a DAO object that gets data from the databae.
add a time stamp field to the DAO.

Option 1) add a table to the database that indicates when the table was last updated. if the time stamp in the DAO/wrapper is older that the database get new data.

Option 2) if current time is more than 24 hours older than that of the updated DAO then fetch new data.


CIAO Peter M. Cooke
Jason Kwok
Ranch Hand

Joined: Mar 31, 2005
Posts: 126
Thanks Peter, that's actually very similar to what I've already done. I have it working already.

My question is... is it a good idea to have the database information saved to the application scope, and as such, each request for information will just feed from the arraylists of information stored in the servlet context?

Or should I have each individual request for information actually summon a servlet and retrieve the specific information from the database?

Is there any difference in processing costs (i.e. performance)? Speed? Will either choice handle multiple requests better?

Thanks again to all!!
Jason
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You'll have to make up a formula based data size, how often it is read, how often it changes, query speed vs response time requirements and so on to see if it says caching is worth while or not. The variables and values in the formula will be pretty personal to you, so we can't predict the final answer for you. In my world for many tables the data size is high and the hit rate is low so caching doesn't look good. For other lookups caching is an obvious option.

When there are updates, say a class list maintenance page, I've seen caches done a couple ways. In one any time some object updates a record it invalidates the record key in the cache. Then the next time somebody needs the data they have to fetch it again. In the other the updater puts new data into the cache. This gets trickier across a cluster. The "invalidate key" technique can make for nice small messages in a pub-sub setup.
[ August 04, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
jQuery in Action, 2nd edition
 
subject: Better practice?