diff --git a/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/AccessDelegate.java b/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/AccessDelegate.java index 5c5635b30dca..a80d5e020924 100644 --- a/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/AccessDelegate.java +++ b/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/AccessDelegate.java @@ -86,6 +86,20 @@ boolean update(Object session, Object key, Object value, Object currentVersion, */ void remove(Object session, Object key) throws CacheException; + /** + * Called just before the delegate will have all entries removed. Any work to prevent concurrent modifications + * while this occurs should happen here + * @throws CacheException if locking had an issue + */ + void lockAll() throws CacheException; + + /** + * Called just after the delegate had all entries removed via {@link #removeAll()}. Any work required to allow + * for new modifications to happen should be done here + * @throws CacheException if unlocking had an issue + */ + void unlockAll() throws CacheException; + /** * Called to evict data from the entire region * diff --git a/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/InvalidationCacheAccessDelegate.java b/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/InvalidationCacheAccessDelegate.java index 6d8546f253f5..2d1e4b01696d 100755 --- a/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/InvalidationCacheAccessDelegate.java +++ b/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/InvalidationCacheAccessDelegate.java @@ -128,18 +128,22 @@ public void remove(Object session, Object key) throws CacheException { } @Override - public void removeAll() throws CacheException { - try { - if (!putValidator.beginInvalidatingRegion()) { - log.failedInvalidateRegion(region.getName()); - } - Caches.removeAll(cache); - } - finally { - putValidator.endInvalidatingRegion(); + public void lockAll() throws CacheException { + if (!putValidator.beginInvalidatingRegion()) { + log.failedInvalidateRegion(region.getName()); } } + @Override + public void unlockAll() throws CacheException { + putValidator.endInvalidatingRegion(); + } + + @Override + public void removeAll() throws CacheException { + Caches.removeAll(cache); + } + @Override public void evict(Object key) throws CacheException { writeCache.remove( key ); diff --git a/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/NonStrictAccessDelegate.java b/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/NonStrictAccessDelegate.java index 7445bcc70f14..968ec98f269d 100644 --- a/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/NonStrictAccessDelegate.java +++ b/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/NonStrictAccessDelegate.java @@ -143,14 +143,18 @@ public void remove(Object session, Object key) throws CacheException { } @Override - public void removeAll() throws CacheException { + public void lockAll() throws CacheException { region.beginInvalidation(); - try { - Caches.broadcastEvictAll(cache); - } - finally { - region.endInvalidation(); - } + } + + @Override + public void unlockAll() throws CacheException { + region.endInvalidation(); + } + + @Override + public void removeAll() throws CacheException { + Caches.broadcastEvictAll(cache); } @Override diff --git a/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/NonTxInvalidationCacheAccessDelegate.java b/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/NonTxInvalidationCacheAccessDelegate.java index 9455098fb389..35767f64b7c3 100644 --- a/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/NonTxInvalidationCacheAccessDelegate.java +++ b/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/NonTxInvalidationCacheAccessDelegate.java @@ -119,15 +119,7 @@ public boolean afterUpdate(Object session, Object key, Object value, Object curr @Override public void removeAll() throws CacheException { - try { - if (!putValidator.beginInvalidatingRegion()) { - log.failedInvalidateRegion(region.getName()); - } - cache.clear(); - } - finally { - putValidator.endInvalidatingRegion(); - } + cache.clear(); } protected void registerLocalInvalidation(Object session, Object lockOwner, Object key) { diff --git a/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/PutFromLoadValidator.java b/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/PutFromLoadValidator.java index 2e7c13db03ee..9a34d0006852 100644 --- a/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/PutFromLoadValidator.java +++ b/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/PutFromLoadValidator.java @@ -450,6 +450,7 @@ public boolean beginInvalidatingRegion() { // removed from the cache and now the DB is about to be updated). for (Iterator it = pendingPuts.values().iterator(); it.hasNext(); ) { PendingPutMap entry = it.next(); + it.remove(); if (entry.acquireLock(60, TimeUnit.SECONDS)) { try { entry.invalidate(now); diff --git a/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/TombstoneAccessDelegate.java b/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/TombstoneAccessDelegate.java index af0da351b515..e69f5956cd92 100644 --- a/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/TombstoneAccessDelegate.java +++ b/hibernate/cache-commons/src/main/java/org/infinispan/hibernate/cache/commons/access/TombstoneAccessDelegate.java @@ -134,14 +134,18 @@ protected void write(Object session, Object key, Object value) { } @Override - public void removeAll() throws CacheException { + public void lockAll() throws CacheException { region.beginInvalidation(); - try { - Caches.broadcastEvictAll(cache); - } - finally { - region.endInvalidation(); - } + } + + @Override + public void unlockAll() throws CacheException { + region.endInvalidation(); + } + + @Override + public void removeAll() throws CacheException { + Caches.broadcastEvictAll(cache); } @Override diff --git a/hibernate/cache-commons/src/test/java/org/infinispan/test/hibernate/cache/commons/functional/InvalidationTest.java b/hibernate/cache-commons/src/test/java/org/infinispan/test/hibernate/cache/commons/functional/InvalidationTest.java index 1d06e1b08c9d..53927fbe6f3f 100644 --- a/hibernate/cache-commons/src/test/java/org/infinispan/test/hibernate/cache/commons/functional/InvalidationTest.java +++ b/hibernate/cache-commons/src/test/java/org/infinispan/test/hibernate/cache/commons/functional/InvalidationTest.java @@ -36,7 +36,7 @@ * @author Radim Vansa <rvansa@redhat.com> */ public class InvalidationTest extends SingleNodeTest { - static final InfinispanMessageLogger log = InfinispanMessageLogger.Provider.getLog(ReadOnlyTest.class); + static final InfinispanMessageLogger log = InfinispanMessageLogger.Provider.getLog(InvalidationTest.class); @Override public List getParameters() { @@ -149,7 +149,12 @@ public void testConcurrentRemoveAndPutFromLoad() throws Exception { }); } - protected AdvancedCache getPendingPutsCache(Class entityClazz) { + protected AdvancedCache getSecondLevelCache(Class entityClazz) { + InfinispanBaseRegion region = TEST_SESSION_ACCESS.getRegion(sessionFactory(), entityClazz.getName()); + return region.getCache(); + } + + protected AdvancedCache getPendingPutsCache(Class entityClazz) { InfinispanBaseRegion region = TEST_SESSION_ACCESS.getRegion(sessionFactory(), entityClazz.getName()); AdvancedCache entityCache = region.getCache(); return entityCache.getCacheManager().getCache( diff --git a/hibernate/cache-v51/src/main/java/org/infinispan/hibernate/cache/v51/access/AbstractAccess.java b/hibernate/cache-v51/src/main/java/org/infinispan/hibernate/cache/v51/access/AbstractAccess.java index bcc5d5d16f56..595fe58da79b 100644 --- a/hibernate/cache-v51/src/main/java/org/infinispan/hibernate/cache/v51/access/AbstractAccess.java +++ b/hibernate/cache-v51/src/main/java/org/infinispan/hibernate/cache/v51/access/AbstractAccess.java @@ -38,10 +38,12 @@ public void removeAll() throws CacheException { } public SoftLock lockRegion() throws CacheException { + delegate.lockAll(); return null; } public void unlockRegion(SoftLock lock) throws CacheException { + delegate.unlockAll(); } } diff --git a/hibernate/cache-v53/src/main/java/org/infinispan/hibernate/cache/v53/impl/AbstractAccess.java b/hibernate/cache-v53/src/main/java/org/infinispan/hibernate/cache/v53/impl/AbstractAccess.java index 3f27d068a7a9..f9b3f2864391 100644 --- a/hibernate/cache-v53/src/main/java/org/infinispan/hibernate/cache/v53/impl/AbstractAccess.java +++ b/hibernate/cache-v53/src/main/java/org/infinispan/hibernate/cache/v53/impl/AbstractAccess.java @@ -44,10 +44,12 @@ public void removeAll(SharedSessionContractImplementor session) throws CacheExce } public SoftLock lockRegion() throws CacheException { + delegate.lockAll(); return null; } public void unlockRegion(SoftLock lock) throws CacheException { + delegate.unlockAll(); } public AccessType getAccessType() {