How about the following code:

// ReentrantLock lock;
lock.acquire();
Object val=cache.get(key);
if(val == null) {
  val=fetchValueFromDB();
  cache.put(node, key, val);
}
lock.release();

This simply uses external synchronization.

A more elegant solution would be to use a CacheLoader: a get() fetches the 
value from the DB and adds it to the cache with a write-lock. CacheLoader 
synchronizes access, so only 1 thread actually triggers the loading.

View the original post : 
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3867468#3867468

Reply to the post : 
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3867468


-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
JBoss-Development mailing list
JBoss-Development@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to