• Bug
  • Status: Closed
  • 2 Major
  • Resolution: Fixed
  • ehcache-core
  • alexsnaps
  • Reporter: alexsnaps
  • December 13, 2010
  • 0
  • Watchers: 1
  • July 27, 2012
  • January 18, 2011


Cache.putInternal when handling a putWithWriter method call does check for whether the key is in the store here :

   boolean elementExists = false;
   try {
       elementExists = compoundStore.containsKey(element.getObjectKey());
       elementExists = !compoundStore.putWithWriter(element, cacheWriterManager) || elementExists;
       if (elementExists) {
       notifyPutInternalListeners(element, doNotNotifyCacheReplicators, elementExists);
   } catch (CacheWriterManagerException e) {
       if (configuration.getCacheWriterConfiguration().getNotifyListenersOnException()) {
           notifyPutInternalListeners(element, doNotNotifyCacheReplicators, elementExists); // Notify on exception!
       throw e.getCause();

The way I read this, is because we (might!) still need to notify the listeners on CacheWriterManagerException, specifying whether the element was there or not. This is important, because if the WriteManager throws an exception in this Store.putWithWriter method:

public boolean putWithWriter(Element element, CacheWriterManager writerManager) { boolean result = put(element); if (writerManager != null) { writerManager.put(element); } return result; }

The element will still be in the store. Updated or newly inserted… Sadly not only is the code calling the unfortunate containsKey method, but there is also a race in that code: between the Store.containsKey and Store.putWithWriter a lot could happen. I propose to add a specific checked exception to Store.putWithWriter that would wrap the initial CacheWriterManagerException and also contain whether the elementExists boolean returned by the Store.put method call from that Store.putWithWriter call.


Alexander Snaps 2011-01-18

Introduced dedicated Exception recording the cause & whether we updated or inserted the Element in the store