Data Caching Question


Performance and scalability: Data Caching Question

  1. Data Caching Question (7 messages)

    Hi All,

    I am not sure if this is the right place to ask this question. Please forgive me.
    Well, I am writing a struts based web application that uses some "almost Static" Data from the DataBase. Since Tomcat is the Servlet Engine in use. I would like to be able to retrieve this data from the DataBase into a file when Tomcat starts up. That way I don't have to wast a call to DataBase on the Data that I know has not changed.

    My own lame solution:
    To create a class that extends the org.apache.struts.action.ActionServlet, retrieve the damn data from the DB Store it in a file(XML, Properties, or..).
    But I am not sure if this the a good solution. Thus,
    Can any of you experts shed some light on this?
    Thank you

    Threaded Messages (7)

  2. Data Caching Question[ Go to top ]

    Your approach is basically OK. Rather than extending the ActionServlet (which may have unexpected effects in the long run), I suggest you create your own independent InitServlet:

    public class InitServlet extends HttpServlet {
      public void init() {
        // Load data from DB or whatever ...

    Then configure your web.xml file to load (and initialize) this servlet to load on startup:


    One final option: if the data really does not change, and it relatively small in size, you can store this information in memory, in the ServletContext. That will be super-fast.

    public class InitServlet extends HttpServlet {
      public void init() {
        HashMap data = // Load data from DB or whatever ...
        getServletContext.setAttribute("static_data", data);

    // To retrieve data in a servlet:
    HashMap data = (HashMap) getServletContext.getAttribute("static_data");

    // To retrieve data in a JSP:
    <jsp:useBean id="static_data" class="java.util.HashMap" scope="application" />

    You can use some other data-storage mechanism than a HashMap; I just picked that as a common example.
  3. Data Caching Question[ Go to top ]


    Thank you very much for the quick response. your suggestions make perfect sense. However, I am concerned that this new InitServlet will interfere with org.apache.struts.action.ActionServlet that is the Core of my MVC based web application. I hope I am wrong
  4. Data Caching Question[ Go to top ]

    There is no reason I can think of why the InitServlet should interfere with the Struts action servlet (or any other servlet for that matter). The two operate pretty much independently.
  5. Data Caching Question[ Go to top ]

    Thanks Paul.
  6. Easier solution.[ Go to top ]


    why doing it so difficult, when it can be easy:

    /* I did not check whether this is actually runable */
    public class DataCache {
      private static Hashtable cache = null;

      private DataCache(){}

      private static synchronized void load(){
        if (null == cache) {
          /* fill hashtable here */

      public static Object getData(Object key) {
        if (null == cache) {
        return cache.get(key);

    To retrieve data, call
    YourDataObject result = (YourDataObject) DataCache.getData(key);

    This lazy-loads all data upon first request. You sure can make it more sophisticated by
     1) Loading entries one by one after checking with containsKey(). But this means calling the DB multiple times.
     2) Expiring Data after n calls to getData() with some sort of replacement strategy (LRU, FIFO, RNU, LFU, etc.). This will limit the amount of memory used to a sane amount.

    Hope this helps,
  7. plugins in struts[ Go to top ]

    This is what struts Plugins are for. They can run initialization at system startup. Note that if you wish lazy loading then this would not be the correct solution, but if you want to execute some code at startup then use the Plugins.
  8. plugins in struts[ Go to top ]


    What struts plug-ing are refering to? can you please elaborate on the idea more?