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

View File

@ -22,6 +22,7 @@
*/ */
package org.hibernate.cache.infinispan.util; package org.hibernate.cache.infinispan.util;
import java.util.Iterator;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import javax.transaction.Status; import javax.transaction.Status;
import javax.transaction.TransactionManager; import javax.transaction.TransactionManager;
@ -265,4 +266,17 @@ public class Caches {
.clustering().cacheMode().isClustered(); .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();
}
}
} }