I have a JSP page that contains 3 select boxes that I need populated with dynamic content. What's the best method for preloading form select boxes with name/values? The information doesn't change very often so I was wondering if I could load each collection into a application/global scope and loop over the collection as needed in my jsp's without hitting the database/server.
Absolutely. Why hit the DB each time if the data will not change? If your app needs to be sensitive to the occasional DB change, the cached data needs to be able to detect that and reload accordingly, but that's a frequently used and understood pattern if you need to employ it.
The application I am working on requiries loading of selection box with data from the database. We are currently loading the data by making a database call to the database for each request. This is making the page loading very slow.Your suggestion of getting the data once from database and storing in the application scope will make the application fast. But if there is any change in the database data how to detect the change and reload the data. Please provide the link to the pattern which you mentioned.
Can you give us some more details as to how you are caching? How is the cache served across concurrent access. Assuming that you are using a servlet which will take care of all requests from the JSP, you can cache the data at the application level, by putting in the information as <attribute,value> in the servlet session context. You can retrieve any data by accessing the attribute in the sessionctx. Once an change happens in the data, the code should automatically modify the corresponding attribute in the servlet. You can remove it using the removeAttribute() call in javax.servlet.ServletContext. You can recreate the value using putAttribute(). That way the data in the cache is refreshed. Your JSP can then be rendered with the new data.
Joined: Apr 25, 2004
What I wanted to know is how do I detect a change in the data in the database table. Whether the data is stored in the appication scope or session scope, it has to be updated as and when the data is updated or new row is added to the database table. Is it done by periodically doing a query on the database and updating the values stored or is there any standard pattern which is used for this.
One easy way of doing it would be to have a single object that is responsible for updating the database for that table, and for caching the retrieved values. This object will refresh the cache as well, whenever it is asked to do an update. All other objects must do the database access for that table through this 'manager' object. You must make sure that there is only one instance of this 'manager' object in the application.
Joined: Apr 25, 2004
The approach of using a manager object such as a DAO works fine if the database update is through the same application. But in my application, the database updates are through a different application and my application just allows to do a query on those tables using some search criteria.