HHH-9781 HHH-9776 Backport clear/evict and element count fixes to 4.3

This commit is contained in:
Galder Zamarreño 2015-05-11 10:23:10 +02:00
parent aa5c8fa122
commit d9ba5502e2
3 changed files with 25 additions and 11 deletions

View File

@ -220,7 +220,7 @@ public class TransactionalAccessDelegate {
if ( !putValidator.invalidateRegion() ) {
throw new CacheException( "Failed to invalidate pending putFromLoad calls for region " + region.getName() );
}
cache.clear();
Caches.removeAll(cache);
}
/**

View File

@ -60,7 +60,7 @@ public abstract class BaseRegion implements Region {
}
private final String name;
private final AdvancedCache regionClearCache;
private final AdvancedCache localAndSkipLoadCache;
private final TransactionManager tm;
private final Object invalidationMutex = new Object();
@ -84,8 +84,9 @@ public abstract class BaseRegion implements Region {
this.name = name;
this.tm = cache.getTransactionManager();
this.factory = factory;
this.regionClearCache = cache.withFlags(
Flag.CACHE_MODE_LOCAL, Flag.ZERO_LOCK_ACQUISITION_TIMEOUT
this.localAndSkipLoadCache = cache.withFlags(
Flag.CACHE_MODE_LOCAL, Flag.ZERO_LOCK_ACQUISITION_TIMEOUT,
Flag.SKIP_CACHE_LOAD
);
}
@ -97,7 +98,7 @@ public abstract class BaseRegion implements Region {
@Override
public long getElementCountInMemory() {
if ( checkValid() ) {
return cache.size();
return localAndSkipLoadCache.size();
}
return 0;
@ -170,27 +171,26 @@ public abstract class BaseRegion implements Region {
// (without forcing autoCommit cache configuration).
Transaction tx = getCurrentTransaction();
if ( tx != null ) {
regionClearCache.clear();
log.tracef("Transaction, clearing one element at the time");
Caches.removeAll(localAndSkipLoadCache);
} else {
Caches.withinTx( cache, new Callable<Void>() {
@Override
public Void call() throws Exception {
regionClearCache.clear();
localAndSkipLoadCache.clear();
return null;
}
} );
}
log.tracef("Transition state from CLEARING to VALID");
invalidateState.compareAndSet(
InvalidateState.CLEARING, InvalidateState.VALID
);
}
catch ( Exception e ) {
if ( log.isTraceEnabled() ) {
log.trace(
"Could not invalidate region: "
+ e.getLocalizedMessage()
);
log.trace("Could not invalidate region: ", e);
}
}
}

View File

@ -22,6 +22,7 @@
*/
package org.hibernate.cache.infinispan.util;
import java.util.Iterator;
import java.util.concurrent.Callable;
import javax.transaction.Status;
import javax.transaction.TransactionManager;
@ -265,4 +266,17 @@ public class Caches {
.clustering().cacheMode().isClustered();
}
public static void removeAll(AdvancedCache cache) {
try {
Iterator it = cache.keySet().iterator();
while (it.hasNext()) {
it.next(); // Necessary to get next element
it.remove();
}
} catch (UnsupportedOperationException e) {
// Fallback on using clear for older version
cache.clear();
}
}
}