Tatyana Maximovskaya
Tatyana Maximovskaya

Reputation: 329

The best place to store large data retrieved by a java servlet (Tomcat)

I have the java servlet that retrieves data from a mysql database. In order to minimize roundtrips to the database, it is retrieved only once in init() method, and is placed to a HashMap<> (i.e. cached in memory).

For now, this HashMap is a member of the servlet class. I need not only store this data but also update some values (counters in fact) in the cached objects of underlying hashmap value class. And there is a Timer (or Cron task) to schedule dumping these counters to DB.

So, after googling i found 3 options of storing the cached data:

1) as now, as a member of servlet class (but servlets can be taken out of service and put back into service by the container at will. Then the data will be lost)

2) in ServletContext (am i right that it is recommended to store small amounts of data here?)

3) in a JNDI resource.

What is the most preferred way?

Upvotes: 4

Views: 2777

Answers (4)

Czar
Czar

Reputation: 1693

If your cache can become large enough and you access it often it'll be reasonable to utilize some caching solution. For example ehcache is a good candidate and easily integrated with Spring applications, too. Documentation is here.

Also check this overview of open-source caching solutions for Java.

Upvotes: 1

BalusC
BalusC

Reputation: 1108702

From those 3 options, the best is to store it in the application scope. I.e. use ServletContext#setAttribute(). You'd like to use a ServletContextListener for this. In normal servlets you can access the ServletContext by the inherited getServletContext() method. In JSP you can access it by ${attributename}.

If the data is getting excessive large that it eats too much of Java's memory, then you should consider a 4th option: use a cache manager.

Upvotes: 3

Bozho
Bozho

Reputation: 597076

Put it in ServletContext But use ConcurrentHashMap to avoid concurrency issues.

Upvotes: 5

locka
locka

Reputation: 6029

The most obvious way would be use something like ehcache and store the data in that. ehcache is a cache manager that works much like a hash map except the cache manager can be tweaked to hold things in memory, move them to disk, flush them, even write them into a database via a plugin etc. Depends if the objects are serializable, and whether your app can cope without data (i.e. make another round trip if necessary) but I would trust a cache manager to do a better job of it than a hand rolled solution.

Upvotes: 2

Related Questions