tags:

views:

39

answers:

1

I have a list of constant values which are stored in a database table. My application's search form is generated by a common include file which can be placed in any JSP page on the site. This search form should include the constants from the database table. A Spring bean manages the database layer and I'm having trouble finding an appropriate place to load the constants from. I have tried the following:

  1. Using a static method to load the constants. The problem with this technique is that a static method cannot access the Spring bean used to access the database.
  2. Loading the constants in the base action class. This would cause the database to be queried for the constants on each page load. I could reduce the performance penalty slightly by saving the results in each user's session, but that is not ideal.
  3. Creating a global spring bean with the constants. I don't know how I would be able to access this global class from the include file though.

Is there another option that I'm overlooking? I would like to be able to cleanly retrieve the constants from the include file.

A: 

What you need is a cache. So what you can do is use a static field in case #2. When this field is null, then you fetch the data and save it in the field. Problem: When the DB changes, the field won't update unless you clear the field or restart the app.

As for #3, you can't access the global class but you can access the bean like any other bean by name. And again, you'll have the issue of staleness.

To solve this, add a second field which contains a timestamp (long). When you change the field, set it to System.currentTimeMillis() plus one day. When you go through the include file, check the timestamp against the current time. When it's older, fetch the values again.

Aaron Digulla