views:

619

answers:

2

Does ehcache support multi-threading by default or does it require any configuration changes? On multi threading my application with Ehcache i found that the DB hit count is actually increasing i.e. there is no global cache available for all the threads despite the fact that my cache's are all Singletons. Any suggestions?

A: 

It sounds to me like you're creating CacheManagers per-thread, rather than creating one and passing it around.

e.g.

CacheManager singletonManager = CacheManager.create();
// and pass this around
Brian Agnew
i am using a single cache manager instance which is handled by spring and i that too is singleton.
hakish
And you've confirmed that's a singleton ? Can you post example code so we have a clearer idea of what's going on ?
Brian Agnew
this is just the flow: XYZ.java ArrayList abc; //this contains all the records to process i.e. objects that implement the Callable interface for(....){ Callable c = abc.get(i); //Initialising the callable object getCompletionService().submit(c); //ExecutorCompletionService instantiates the thread to prcocess } AppContext.xml <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml"/></bean>
hakish
+1  A: 

This may help answer your question, from the FAQ:

Is it thread safe to modify Element values after retrieval from a Cache?

Remember that a value in a cache element is globally accessible from multiple threads. (It is inherently not thread safe to modify the value.* It is safer to retrieve a value, delete the cache element and then reinsert the value.

(emphasis added by me)

matt b