title: Simple caching mechanism using hashmap and syncronized method call
body: <p>We have a simple caching mechanism using HashMaps and syncronized load method. This is for a WAS 8 application server. Application consists of several web applications and web methods using JPA in background.<P>
  Since yesterday it was working nicely and had no problems. But got a major downtime. Reason was during some high traffic of webservices, lots of loadData method calls occured. I believe maps were null by that time, and every call tried to execute loaddata method even if first call loaded map.
  <p> A simple workaround came to my mind is seperately calling each load method synchronized seperately. But ultimately i believe we need a better solution since this will only lessen the chances that this will occur again.<P>
  I am aware there are some apis for caching, and willing to dig into more. But i believe there is a/some good solution without an api.<p>
  So, what would you suggest for this situation?</p>
  
  <p><strong>Edit:</strong> loadData method is only called from a servlet manually if cache needs to reloaded.</p>
  
  <pre><code>    private static HashMap&lt;String, Long&gt; someObjectMap = new HashMap&lt;String, Long&gt;();
  private static HashMap&lt;String, Long&gt; someAnotherObjectMap = new HashMap&lt;String, Long&gt;();
  
  
  public synchronized static void loadData() {
      loadSomeObjectMap();
      loadSomeAnotherObjectMap();
              // and some more methods similar, about 10 of them
  }
  
  //fill first map
  public static void loadSomeObjectMap (){
      someObjectMap = null;
      try {
          if (someObjectMap == null ) {
              someObjectMap = new HashMap&lt;String, Long&gt;();
              //fill someObjectMap here
          }
      } catch (Exception e) {
          logger.error(Util.getStackTrace(e));
      }       
  }
  //fill second map
  public static void loadSomeAnotherObjectMap (){
      someAnotherObjectMap = null;
      try {
          if (someAnotherObjectMap == null ) {
              someAnotherObjectMap = new HashMap&lt;String, Long&gt;();
              //fill someObjectMap here
          }
      } catch (Exception e) {
          logger.error(Util.getStackTrace(e));
      }       
  }
  </code></pre>