This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

Caching data base results

Seby Pappachan
Greenhorn

Joined: Nov 02, 2008
Posts: 12
Hi,

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.

Thanks
Seby
hildich kilal
Ranch Hand

Joined: Aug 25, 2008
Posts: 44
Seby,

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: 16095
    
  21

There's no need to re-invent caching. There are some perfectly good pre-built (and pre-debugged) resources at jakarta.apache.org. 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.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
GeeCON Prague 2014
 
subject: Caching data base results