cancel
Showing results for 
Search instead for 
Did you mean: 

implementing cache for dropdown values in Web Dynpro Iview

Former Member
0 Kudos

Hi All,

I am currently in the processing of enhancing a web dynpro application which contains among other things around 15 drop down boxes. The values in these drop down boxes are coming from oracle database and these values change occasionally.

To optimize the response time, I have implemented simple caching machanism using static variable in plain java class. The objective is to retrieve the values for the first time from oracle db and use the same datastructure for subsequent calls. Though I have found that the number of calls to the database reduced significantly I am facing some problem understanding and implementing the cache refresh behaviour.

I want to implement a cache refresh machanism for every 12 hours.

Solutions tried.

Creating a thread to refresh the cache for every 12 hours.

Creating a timer for refreshing the cache for every 12 hours.

Problems encountered :

1. Is it appropriate to use threads in a web dynpro app?

2. What I have observed is that the thread (I have created a daemon thread) is alive even after I have deployed a new copy of the code. When I deploy a new code is it not supposed to remove all copies from the memory?

If using a daemon thread is appropriate, What is the web dynpro

framework's class loading behavior when a new copy of code is deployed?

Does it completely unload existing classes (there by killing the daemon thread

created in previous deployment)?

3. Assuming that we have found suitable solution for thread issues, what would happen when the application is deployed on a cluster? Can we send a message to

all the nodes in the cluster?

I would like to understand what other developers has done in these kind of situations. Your experience and insight will be valuable and help me decide to implement caching or not in the first place.

Thanks in advance.

Regards

Pallayya Batchu

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Pallayya,

<i>1. Is it appropriate to use threads in a web dynpro app?</i>

Not recommended as with any J2EE application

<i>2. What I have observed is that the thread (I have created a daemon thread) is alive even after I have deployed a new copy of the code. When I deploy a new code is it not supposed to remove all copies from the memory?</i>

Re-deployment doesn't mean stopping all user spawned threads. It just causes unloading of classes if there are no hard references from anything but deployed application. In your case, there are probably references from Thread/Runnable so your previous version is not unloaded on redeployment.

<i>3. Assuming that we have found suitable solution for thread issues, what would happen when the application is deployed on a cluster? Can we send a message to all the nodes in the cluster?</i>

Probably you can, probably you cannot. Even if you can it would be complex.

My advise -- abandon threads altogether, use real cache instead:


package com.yourcompany.yourapp.utils;

import java.util.HashMap;
import java.util.Map;

public class ValueHelpCache {
  
  private static class Entry {
    long lastLoadTime;
    Map  payload;
	  
    Entry(final Map payload) { 
      this.payload = payload;
      this.lastLoadTime = System.currentTimeMillis();
    }
  }
	
  final private Map _entries = new HashMap();
  
  private ValueHelpCache() {}
  
  synchronized public Map getValueHelp(final String valuyeHelpKey) {
     Entry entry = (Entry)_entries.get(valuyeHelpKey);
     if ( entry == null) {
       entry = new Entry( loadValueHelpFromDatabase(valuyeHelpKey) );
       _entries.put(valuyeHelpKey, entry);
     } else {
       final long now = System.currentTimeMillis();
       if ( now - entry.lastLoadTime > ENTRY_TTL ) {
         entry.payload = loadValueHelpFromDatabase(valuyeHelpKey);
         entry.lastLoadTime = now;
       }
    }
    return entry.payload;
  }
  
  private Map loadValueHelpFromDatabase(final String valuyeHelpKey) {
    /* @TODO implement loading values from database */
    return null;
  }

  public static ValueHelpCache getInstance() { return INSTANCE; }

  final public static long ENTRY_TTL = 12 * 60 * 60 * 1000;
  
  final private static ValueHelpCache INSTANCE = new ValueHelpCache();
}

This way client code tracks itself what entries are stale and need to be reloaded. No threads at all and no problems in cluster. You may alter time tracking mechanism to reload at given time of day, say at 12AM and 12PM -- just use java.util.Calendar and change code accordingly.

Valery Silaev

SaM Solutions

http://www.sam-solutions.net

Former Member
0 Kudos

Thanks Valery.

I think this is a good solution. Let me try it.

Thanks again.

Regards

Pallayya Batchu

Former Member
0 Kudos

Hello Pallayya Batchu,

A simple way to implement a cache is by using the system time. Every time the view is rendered you call the cache to retrieve the values. The cache internally holds the time it was last refreshed and you check if the cache period has passed. If so you get the new values from the db, if not you keep the values from the cache.

I have no idea what the behavior is when you use threads in a web dynpro application.

Hope this helps.

Regards,

Christophe