File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Caching data base results Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Caching data base results" Watch "Caching data base results" New topic

Caching data base results

Seby Pappachan

Joined: Nov 02, 2008
Posts: 12

I am developing a tool for my team. This a tool which is used for monitoring purpose(restricted explanation). Even if "n" number of users are logged to the system, the data displayed to the users will be same irrespective of the users. These details are queried from the database and displayed to the user. But this data is not static always. This will be changing each one hour. Means, in the back end a scheduler is running which is updating the DB in every hour.

So, for a particular period of time (maximum 1 hour), the data will be constant. As per the current design, for each user i am querying the DB and displaying the details. This is nothing more than performance reduction.

I am using JSP to display the details. Is there any mechanism to query the db and put the data in cache or some where else(this should get updated in each hour) so that i can use those values without having a db connection?

Note : I do not want the details to be stored in text,xml,CSV etc.

Please let me know if you need further details.

hildich kilal
Ranch Hand

Joined: Aug 25, 2008
Posts: 44

You can simply implement a caching mechanism yourself. Search for Implementing Caching in google. This singleton class cache you can store in Application context , thus it will be accessible from any user login.
Or rather than storing in context you can just make it a singleton class initialized in a separate Servlet (Call it a ApplicationInitializerServlet) which gets loaded up when application is loaded in Servlet container.
In the cache class you can create a Thread (You can use a Schedulre or timer class or New ThreadExecutor class in java 5) to create a scheduler thread which will keep on looing for any change in the particular DB you are expecting data to change , As soon as it finds new updates in DB yuor thread can invoke JDBC calls to update the cache class object.

You can use HashMap or HashTable as per your convenience.

Also if you want you can use Hibernate to do all this caching and periodic lookup of database change. It relieves you of creating a continuous running thread to monitor DB change. As soon as any table data changes the Hibernate cache objcets are updated on their own.

but i will advise avoid Hibernate for this small requirement and create your own Cache classes.

Hope this helps
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

There's no need to re-invent caching. There are some perfectly good pre-built (and pre-debugged) resources at In fact, I think that you'll find them being used as the caching mechanism for both open-source ORM products like Hibernate and commercial ones.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: Caching data base results
It's not a secret anymore!