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
-
Data Caching Question (7 messages)
- Posted by: Atlas Casa
- Posted on: August 21 2003 18:12 EDT
Threaded Messages (7)
- Data Caching Question by Paul Strack on August 21 2003 23:07 EDT
- Data Caching Question by Atlas Casa on August 22 2003 11:12 EDT
-
Data Caching Question by Paul Strack on August 22 2003 07:53 EDT
- Data Caching Question by Atlas Casa on August 25 2003 09:37 EDT
-
Data Caching Question by Paul Strack on August 22 2003 07:53 EDT
- Easier solution. by Lars Stitz on August 28 2003 05:31 EDT
- Data Caching Question by Atlas Casa on August 22 2003 11:12 EDT
- plugins in struts by bruce goldstein on October 10 2003 11:56 EDT
- plugins in struts by Atlas Casa on October 10 2003 16:07 EDT
-
Data Caching Question[ Go to top ]
- Posted by: Paul Strack
- Posted on: August 21 2003 23:07 EDT
- in response to Atlas Casa
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:
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>[package].InitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
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. -
Data Caching Question[ Go to top ]
- Posted by: Atlas Casa
- Posted on: August 22 2003 11:12 EDT
- in response to Paul Strack
Paul,
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 -
Data Caching Question[ Go to top ]
- Posted by: Paul Strack
- Posted on: August 22 2003 19:53 EDT
- in response to Atlas Casa
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. -
Data Caching Question[ Go to top ]
- Posted by: Atlas Casa
- Posted on: August 25 2003 09:37 EDT
- in response to Paul Strack
Thanks Paul. -
Easier solution.[ Go to top ]
- Posted by: Lars Stitz
- Posted on: August 28 2003 05:31 EDT
- in response to Paul Strack
Hi,
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) {
load();
}
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,
Lars -
plugins in struts[ Go to top ]
- Posted by: bruce goldstein
- Posted on: October 10 2003 11:56 EDT
- in response to Atlas Casa
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. -
plugins in struts[ Go to top ]
- Posted by: Atlas Casa
- Posted on: October 10 2003 16:07 EDT
- in response to bruce goldstein
Bruce,
What struts plug-ing are refering to? can you please elaborate on the idea more?