@Test public void testPutIfAbsent() throws Exception { transactionManager.begin(); assertEquals("Cache should be empty to start", 0, cache.getSize()); assertEquals("Cach1 should be empty to start", 0, cach1.getSize()); assertNull(cach1.putIfAbsent(new Element("key", "value1"))); assertNull(cache.putIfAbsent(new Element("key", "value1"))); Transaction tx1 = transactionManager.suspend(); transactionManager.begin(); assertNull(cache.putIfAbsent(new Element("key", "value2"))); transactionManager.commit(); transactionManager.resume(tx1); try { transactionManager.commit(); fail("This should have thrown an Exception, as the putIfAbsent should have failed!"); } catch (RollbackException e) { } CacheLockProvider clp = (CacheLockProvider)cache.getInternalContext(); assertFalse(clp.getSyncForKey("key").isHeldByCurrentThread(LockType.WRITE)); transactionManager.begin(); Element element = cache.get("key"); assertEquals("value2", element.getValue()); assertNull(cach1.get("key")); assertEquals(element, cache.putIfAbsent(new Element("key", "value3"))); transactionManager.commit(); transactionManager.begin(); element = cache.get("key"); assertEquals("value2", element.getValue()); transactionManager.commit(); transactionManager.begin(); cache.put(new Element("key2", "randomValue")); cache.putIfAbsent(new Element("key2", "notThere!")); assertEquals("randomValue", cache.get("key2").getValue()); transactionManager.commit(); transactionManager.begin(); assertEquals("randomValue", cache.get("key2").getValue()); transactionManager.commit(); transactionManager.begin(); cach1.remove("key2"); cache.remove("key2"); cache.putIfAbsent(new Element("key2", "nowThere!")); assertEquals("nowThere!", cache.get("key2").getValue()); transactionManager.commit(); transactionManager.begin(); assertEquals("nowThere!", cache.get("key2").getValue()); transactionManager.commit(); transactionManager.begin(); cache.remove("key2"); cache.putIfAbsent(new Element("key2", "nowThere!")); assertEquals("nowThere!", cache.get("key2").getValue()); Transaction tx2 = transactionManager.suspend(); transactionManager.begin(); assertEquals("nowThere!", cache.get("key2").getValue()); cache.put(new Element("key2", "newValue")); transactionManager.commit(); transactionManager.resume(tx2); cach1.put(new Element("fake", "entry")); try { transactionManager.commit(); fail("This should have thrown an Exception!"); } catch (RollbackException e) { } transactionManager.begin(); assertEquals("newValue", cache.get("key2").getValue()); transactionManager.commit(); }
public Element putIfAbsent(Element element) throws NullPointerException { TransactionContext context = getOrCreateTransactionContext(); Element previous = getCurrentElement(element.getKey(), context); if (previous == null) { context.addCommand(new StorePutIfAbsentCommand(element), element); } return previous; }
XATransactionalStore extends AbstractStore { public Element putIfAbsent(Element element) throws NullPointerException { TransactionContext context = getOrCreateTransactionContext(); Element previous = getCurrentElement(element.getKey(), context); if (previous == null) { context.addCommand(new StorePutIfAbsentCommand(element), element); } return previous; } }
XATransactionalStore extends AbstractStore { public Element putIfAbsent(Element element) throws NullPointerException { TransactionContext context = getOrCreateTransactionContext(); Element previous = getCurrentElement(element.getKey(), context); if (previous == null) { context.addCommand(new StorePutIfAbsentCommand(element), element); } return previous; } XATransactionalStore(Ehcache cache, EhcacheXAStore ehcacheXAStore,
                                TransactionManagerLookup transactionManagerLookup, TransactionManager txnManager); }
XATransactionalStore extends AbstractStore { public Element putIfAbsent(Element element) throws NullPointerException { TransactionContext context = getOrCreateTransactionContext(); Element previous = getCurrentElement(element.getKey(), context); if (previous == null) { context.addCommand(new StorePutIfAbsentCommand(element), element); } return previous; } XATransactionalStore(Ehcache cache, EhcacheXAStore ehcacheXAStore,
                                TransactionManagerLookup transactionManagerLookup, TransactionManager txnManager); boolean put(final Element element); boolean putWithWriter(final Element element, final CacheWriterManager writerManager); Element get(final Object key); Element getQuiet(final Object key); final List getKeys(); Element remove(final Object key); Element removeWithWriter(final Object key, final CacheWriterManager writerManager); void removeAll(); void dispose(); int getSize(); int getOnDiskSize(); int getOffHeapSize(); int getInMemorySize(); int getTerracottaClusteredSize(); long getInMemorySizeInBytes(); long getOffHeapSizeInBytes(); long getOnDiskSizeInBytes(); Status getStatus(); boolean containsKey(final Object key); boolean containsKeyInMemory(final Object key); boolean containsKeyOffHeap(final Object key); boolean containsKeyOnDisk(final Object key); void expireElements(); void flush(); boolean bufferFull(); Policy getInMemoryEvictionPolicy(); void setInMemoryEvictionPolicy(final Policy policy); Object getInternalContext(); @Override boolean isCacheCoherent(); @Override boolean isClusterCoherent(); @Override boolean isNodeCoherent(); @Override void setNodeCoherent(boolean coherent); @Override void waitUntilClusterCoherent(); Element putIfAbsent(Element element); Element removeElement(Element element); boolean replace(Element old, Element element); Element replace(Element element); EhcacheXAResource getOrCreateXAResource(); Object getMBean(); }
XATransactionalStore extends AbstractStore { public Element putIfAbsent(Element element) throws NullPointerException { TransactionContext context = getOrCreateTransactionContext(); Element previous = getCurrentElement(element.getKey(), context); if (previous == null) { context.addCommand(new StorePutIfAbsentCommand(element), element); } return previous; } XATransactionalStore(Ehcache cache, EhcacheXAStore ehcacheXAStore,
                                TransactionManagerLookup transactionManagerLookup, TransactionManager txnManager); boolean put(final Element element); boolean putWithWriter(final Element element, final CacheWriterManager writerManager); Element get(final Object key); Element getQuiet(final Object key); final List getKeys(); Element remove(final Object key); Element removeWithWriter(final Object key, final CacheWriterManager writerManager); void removeAll(); void dispose(); int getSize(); int getOnDiskSize(); int getOffHeapSize(); int getInMemorySize(); int getTerracottaClusteredSize(); long getInMemorySizeInBytes(); long getOffHeapSizeInBytes(); long getOnDiskSizeInBytes(); Status getStatus(); boolean containsKey(final Object key); boolean containsKeyInMemory(final Object key); boolean containsKeyOffHeap(final Object key); boolean containsKeyOnDisk(final Object key); void expireElements(); void flush(); boolean bufferFull(); Policy getInMemoryEvictionPolicy(); void setInMemoryEvictionPolicy(final Policy policy); Object getInternalContext(); @Override boolean isCacheCoherent(); @Override boolean isClusterCoherent(); @Override boolean isNodeCoherent(); @Override void setNodeCoherent(boolean coherent); @Override void waitUntilClusterCoherent(); Element putIfAbsent(Element element); Element removeElement(Element element); boolean replace(Element old, Element element); Element replace(Element element); EhcacheXAResource getOrCreateXAResource(); Object getMBean();  }