Merge branch 'master' of github.com:hibernate/hibernate-core

This commit is contained in:
Steve Ebersole 2012-01-11 12:14:26 -06:00
commit 7899d34ca4
33 changed files with 636 additions and 594 deletions

View File

@ -29,6 +29,14 @@
on the entity.
</para>
<important>
<para>
Unlike in previous versions, you no longer need to specify listeners in the Hibernate configuration
file. Just putting the Envers jar on the classpath is enough - listeners will be registered
automatically.
</para>
</important>
<para>
And that's all - you can create, modify and delete the entites as always. If you look at the generated
schema for your entities, or at the data persisted by Hibernate, you will notice that there are no changes.

View File

@ -282,9 +282,9 @@ public class OptimizerFactory {
* <p/>
* The general algorithms used to determine the bucket are:<ol>
* <li>{@code upperLimit = (databaseValue * incrementSize) + 1}</li>
* <li>{@code lowerLimit = upperLimit - 1}</li>
* <li>{@code lowerLimit = upperLimit - incrementSize}</li>
* </ol>
* As an example, consider a case with incrementSize of 10. Initially the
* As an example, consider a case with incrementSize of 20. Initially the
* database holds 1:<ol>
* <li>{@code upperLimit = (1 * 20) + 1 = 21}</li>
* <li>{@code lowerLimit = 21 - 20 = 1}</li>
@ -457,7 +457,9 @@ public class OptimizerFactory {
// we are using a sequence...
if (value.lt(1)) LOG.pooledOptimizerReportedInitialValue(value);
// the call to obtain next-value just gave us the initialValue
if ((initialValue == -1 && value.lt(incrementSize)) || value.eq(initialValue)) hiValue = callback.getNextValue();
if ( ( initialValue == -1 && value.lt( incrementSize ) ) || value.eq( initialValue ) ) {
hiValue = callback.getNextValue();
}
else {
hiValue = value;
value = hiValue.copy().subtract( incrementSize );

View File

@ -5,7 +5,7 @@ configurations {
}
dependencies {
infinispanVersion = '5.1.0.CR1'
infinispanVersion = '5.1.0.CR3'
jnpVersion = '5.0.3.GA'
compile(project(':hibernate-core'))
@ -35,6 +35,8 @@ test {
systemProperties['jgroups.ping.num_initial_members'] = 1
systemProperties['jgroups.udp.enable_bundling'] = false
systemProperties['jgroups.bind_addr'] = 'localhost'
// Use Infinispan's test JGroups stack that uses TEST_PING
systemProperties['hibernate.cache.infinispan.jgroups_cfg'] = 'stacks/tcp.xml'
// systemProperties['log4j.configuration'] = 'file:/log4j/log4j-infinispan.xml'
enabled = true
}

View File

@ -1,4 +1,5 @@
package org.hibernate.cache.infinispan;
import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
@ -7,7 +8,13 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.transaction.TransactionManager;
import org.hibernate.cache.infinispan.impl.BaseRegion;
import org.hibernate.cache.infinispan.util.CacheCommandFactory;
import org.hibernate.cache.infinispan.util.CacheCommandInitializer;
import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.spi.CollectionRegion;
@ -28,8 +35,9 @@ import org.hibernate.cache.infinispan.util.CacheAdapterImpl;
import org.hibernate.cfg.Settings;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.infinispan.AdvancedCache;
import org.infinispan.Cache;
import org.infinispan.config.Configuration;
import org.infinispan.factories.ComponentRegistry;
import org.infinispan.factories.GlobalComponentRegistry;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.util.logging.Log;
@ -156,6 +164,9 @@ public class InfinispanRegionFactory implements RegionFactory {
private TransactionManager transactionManager;
private ConcurrentMap<String, BaseRegion> allRegions =
new ConcurrentHashMap<String, BaseRegion>();
/**
* Create a new instance using the default configuration.
*/
@ -174,20 +185,20 @@ public class InfinispanRegionFactory implements RegionFactory {
/** {@inheritDoc} */
public CollectionRegion buildCollectionRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException {
if (log.isDebugEnabled()) log.debug("Building collection cache region [" + regionName + "]");
Cache cache = getCache(regionName, COLLECTION_KEY, properties);
AdvancedCache cache = getCache(regionName, COLLECTION_KEY, properties);
CacheAdapter cacheAdapter = CacheAdapterImpl.newInstance(cache);
CollectionRegionImpl region = new CollectionRegionImpl(cacheAdapter, regionName, metadata, transactionManager, this);
region.start();
startRegion(region, regionName);
return region;
}
/** {@inheritDoc} */
public EntityRegion buildEntityRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException {
if (log.isDebugEnabled()) log.debug("Building entity cache region [" + regionName + "]");
Cache cache = getCache(regionName, ENTITY_KEY, properties);
AdvancedCache cache = getCache(regionName, ENTITY_KEY, properties);
CacheAdapter cacheAdapter = CacheAdapterImpl.newInstance(cache);
EntityRegionImpl region = new EntityRegionImpl(cacheAdapter, regionName, metadata, transactionManager, this);
region.start();
startRegion(region, regionName);
return region;
}
@ -202,10 +213,10 @@ public class InfinispanRegionFactory implements RegionFactory {
if (!regionName.equals("org.hibernate.cache.internal.StandardQueryCache"))
cacheName = regionName;
Cache cache = getCache(cacheName, QUERY_KEY, properties);
AdvancedCache cache = getCache(cacheName, QUERY_KEY, properties);
CacheAdapter cacheAdapter = CacheAdapterImpl.newInstance(cache);
QueryResultsRegionImpl region = new QueryResultsRegionImpl(cacheAdapter, regionName, properties, transactionManager, this);
region.start();
startRegion(region, regionName);
return region;
}
@ -215,10 +226,10 @@ public class InfinispanRegionFactory implements RegionFactory {
public TimestampsRegion buildTimestampsRegion(String regionName, Properties properties)
throws CacheException {
if (log.isDebugEnabled()) log.debug("Building timestamps cache region [" + regionName + "]");
Cache cache = getCache(regionName, TIMESTAMPS_KEY, properties);
AdvancedCache cache = getCache(regionName, TIMESTAMPS_KEY, properties);
CacheAdapter cacheAdapter = CacheAdapterImpl.newInstance(cache);
TimestampsRegionImpl region = createTimestampsRegion(cacheAdapter, regionName);
region.start();
startRegion(region, regionName);
return region;
}
@ -226,10 +237,6 @@ public class InfinispanRegionFactory implements RegionFactory {
return new TimestampsRegionImpl(cacheAdapter, regionName, transactionManager, this);
}
protected TransactionManager getTransactionManager() {
return transactionManager;
}
/**
* {@inheritDoc}
*/
@ -270,7 +277,7 @@ public class InfinispanRegionFactory implements RegionFactory {
Enumeration keys = properties.propertyNames();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
int prefixLoc = -1;
int prefixLoc;
if ((prefixLoc = key.indexOf(PREFIX)) != -1) {
dissectProperty(prefixLoc, key, properties);
}
@ -310,11 +317,15 @@ public class InfinispanRegionFactory implements RegionFactory {
return Collections.unmodifiableSet(definedConfigurations);
}
public BaseRegion getRegion(String regionName) {
return allRegions.get(regionName);
}
protected EmbeddedCacheManager createCacheManager(Properties properties) throws CacheException {
try {
String configLoc = ConfigurationHelper.getString(INFINISPAN_CONFIG_RESOURCE_PROP, properties, DEF_INFINISPAN_CONFIG_RESOURCE);
EmbeddedCacheManager manager = new DefaultCacheManager(configLoc, false);
String globalStats = ConfigurationHelper.extractPropertyValue(INFINISPAN_GLOBAL_STATISTICS_PROP, properties);
String globalStats = extractProperty(INFINISPAN_GLOBAL_STATISTICS_PROP, properties);
if (globalStats != null) {
manager.getGlobalConfiguration().setExposeGlobalJmxStatistics(Boolean.parseBoolean(globalStats));
}
@ -325,6 +336,10 @@ public class InfinispanRegionFactory implements RegionFactory {
}
}
private void startRegion(BaseRegion region, String regionName) {
allRegions.put(regionName, region);
}
private Map<String, TypeOverrides> initGenericDataTypeOverrides() {
TypeOverrides entityOverrides = new TypeOverrides();
entityOverrides.setCacheName(DEF_ENTITY_RESOURCE);
@ -342,31 +357,33 @@ public class InfinispanRegionFactory implements RegionFactory {
}
private void dissectProperty(int prefixLoc, String key, Properties properties) {
TypeOverrides cfgOverride = null;
int suffixLoc = -1;
TypeOverrides cfgOverride;
int suffixLoc;
if (!key.equals(INFINISPAN_CONFIG_RESOURCE_PROP) && (suffixLoc = key.indexOf(CONFIG_SUFFIX)) != -1) {
cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
cfgOverride.setCacheName( ConfigurationHelper.extractPropertyValue(key, properties));
cfgOverride.setCacheName(extractProperty(key, properties));
} else if ((suffixLoc = key.indexOf(STRATEGY_SUFFIX)) != -1) {
cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
cfgOverride.setEvictionStrategy( ConfigurationHelper.extractPropertyValue(key, properties));
cfgOverride.setEvictionStrategy(extractProperty(key, properties));
} else if ((suffixLoc = key.indexOf(WAKE_UP_INTERVAL_SUFFIX)) != -1) {
cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
cfgOverride.setEvictionWakeUpInterval(Long.parseLong( ConfigurationHelper.extractPropertyValue(key, properties)));
cfgOverride.setEvictionWakeUpInterval(Long.parseLong(extractProperty(key, properties)));
} else if ((suffixLoc = key.indexOf(MAX_ENTRIES_SUFFIX)) != -1) {
cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
cfgOverride.setEvictionMaxEntries( ConfigurationHelper.getInt(key, properties, -1));
cfgOverride.setEvictionMaxEntries(Integer.parseInt(extractProperty(key, properties)));
} else if ((suffixLoc = key.indexOf(LIFESPAN_SUFFIX)) != -1) {
cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
cfgOverride.setExpirationLifespan(Long.parseLong( ConfigurationHelper.extractPropertyValue(key, properties)));
cfgOverride.setExpirationLifespan(Long.parseLong(extractProperty(key, properties)));
} else if ((suffixLoc = key.indexOf(MAX_IDLE_SUFFIX)) != -1) {
cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
cfgOverride.setExpirationMaxIdle(Long.parseLong( ConfigurationHelper.extractPropertyValue(key, properties)));
cfgOverride.setExpirationMaxIdle(Long.parseLong(extractProperty(key, properties)));
}
// else if ((suffixLoc = key.indexOf(STATISTICS_SUFFIX)) != -1) {
// cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
// cfgOverride.setExposeStatistics(Boolean.parseBoolean(PropertiesHelper.extractPropertyValue(key, properties)));
// }
}
private String extractProperty(String key, Properties properties) {
String value = ConfigurationHelper.extractPropertyValue(key, properties);
log.debugf("Configuration override via property %s: %s", key, value);
return value;
}
private TypeOverrides getOrCreateConfig(int prefixLoc, String key, int suffixLoc) {
@ -395,7 +412,7 @@ public class InfinispanRegionFactory implements RegionFactory {
}
}
private Cache getCache(String regionName, String typeKey, Properties properties) {
private AdvancedCache getCache(String regionName, String typeKey, Properties properties) {
TypeOverrides regionOverride = typeOverrides.get(regionName);
if (!definedConfigurations.contains(regionName)) {
String templateCacheName = null;
@ -420,20 +437,27 @@ public class InfinispanRegionFactory implements RegionFactory {
manager.defineConfiguration(regionName, templateCacheName, regionCacheCfg);
definedConfigurations.add(regionName);
}
Cache cache = manager.getCache(regionName);
AdvancedCache cache = manager.getCache(regionName).getAdvancedCache();
if (!cache.getStatus().allowInvocations()) {
cache.start();
}
return createCacheWrapper(cache.getAdvancedCache());
ComponentRegistry cr = cache.getComponentRegistry();
cr.getComponent(CacheCommandInitializer.class).setRegionFactory(this);
GlobalComponentRegistry globalCr = cache.getComponentRegistry().getGlobalComponentRegistry();
// TODO: This is a hack, make it easier to retrieve in Infinispan!
((CacheCommandFactory) ((Map) globalCr.getComponent("org.infinispan.modules.command.factories"))
.values().iterator().next()).setRegionFactory(this);
return createCacheWrapper(cache);
}
protected ClassLoaderAwareCache createCacheWrapper(AdvancedCache cache) {
protected AdvancedCache createCacheWrapper(AdvancedCache cache) {
return new ClassLoaderAwareCache(cache, Thread.currentThread().getContextClassLoader());
}
private Configuration configureTransactionManager(Configuration regionOverrides, String templateCacheName, Properties properties) {
// Get existing configuration to verify whether a tm was configured or not.
Configuration templateConfig = manager.defineConfiguration(templateCacheName, new Configuration());
if (templateConfig.isTransactionalCache()) {
String ispnTmLookupClassName = templateConfig.getTransactionManagerLookupClass();
String hbTmLookupClassName = org.hibernate.cache.infinispan.tm.HibernateTransactionManagerLookup.class.getName();
if (ispnTmLookupClassName != null && !ispnTmLookupClassName.equals(hbTmLookupClassName)) {
@ -443,15 +467,16 @@ public class InfinispanRegionFactory implements RegionFactory {
regionOverrides.setTransactionManagerLookup(transactionManagerlookup);
}
String useSyncProp = ConfigurationHelper.extractPropertyValue(INFINISPAN_USE_SYNCHRONIZATION_PROP, properties);
String useSyncProp = extractProperty(INFINISPAN_USE_SYNCHRONIZATION_PROP, properties);
boolean useSync = useSyncProp == null ? DEF_USE_SYNCHRONIZATION : Boolean.parseBoolean(useSyncProp);
regionOverrides.fluent().transaction().useSynchronization(useSync);
}
return regionOverrides;
}
private TypeOverrides overrideStatisticsIfPresent(TypeOverrides override, Properties properties) {
String globalStats = ConfigurationHelper.extractPropertyValue(INFINISPAN_GLOBAL_STATISTICS_PROP, properties);
String globalStats = extractProperty(INFINISPAN_GLOBAL_STATISTICS_PROP, properties);
if (globalStats != null) {
override.setExposeStatistics(Boolean.parseBoolean(globalStats));
}

View File

@ -22,6 +22,7 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.cache.infinispan.access;
import javax.transaction.Transaction;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
@ -29,7 +30,6 @@ import org.hibernate.cache.spi.access.EntityRegionAccessStrategy;
import org.hibernate.cache.spi.access.SoftLock;
import org.hibernate.cache.infinispan.impl.BaseRegion;
import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cache.infinispan.util.CacheHelper;
import org.hibernate.cache.infinispan.util.FlagAdapter;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
@ -162,7 +162,8 @@ public class TransactionalAccessDelegate {
}
Transaction tx = region.suspend();
try {
CacheHelper.sendEvictAllNotification(cacheAdapter, region.getAddress());
region.invalidateRegion(); // Invalidate the local region and then go remote
cacheAdapter.broadcastEvictAll();
} finally {
region.resume(tx);
}

View File

@ -1,10 +1,8 @@
package org.hibernate.cache.infinispan.impl;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
@ -13,17 +11,9 @@ import org.hibernate.cache.CacheException;
import org.hibernate.cache.spi.Region;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.infinispan.util.AddressAdapter;
import org.hibernate.cache.infinispan.util.AddressAdapterImpl;
import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cache.infinispan.util.CacheHelper;
import org.hibernate.cache.infinispan.util.FlagAdapter;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryInvalidated;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryModified;
import org.infinispan.notifications.cachelistener.event.CacheEntryInvalidatedEvent;
import org.infinispan.notifications.cachelistener.event.CacheEntryModifiedEvent;
import org.infinispan.notifications.cachemanagerlistener.annotation.ViewChanged;
import org.infinispan.notifications.cachemanagerlistener.event.ViewChangedEvent;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
@ -37,12 +27,12 @@ import org.infinispan.util.logging.LogFactory;
* @since 3.5
*/
public abstract class BaseRegion implements Region {
private enum InvalidateState { INVALID, CLEARING, VALID };
private static final Log log = LogFactory.getLog(BaseRegion.class);
private final String name;
protected final CacheAdapter cacheAdapter;
protected final AddressAdapter address;
protected final Set<AddressAdapter> currentView = new HashSet<AddressAdapter>();
protected final TransactionManager transactionManager;
protected final boolean replication;
protected final Object invalidationMutex = new Object();
@ -59,32 +49,6 @@ public abstract class BaseRegion implements Region {
this.factory = factory;
}
public void start() {
if (address != null) {
synchronized (currentView) {
List<AddressAdapter> view = cacheAdapter.getMembers();
if (view != null) {
currentView.addAll(view);
establishInternalNodes();
}
}
}
}
/**
* Calls to this method must be done from synchronized (currentView) blocks only!!
*/
private void establishInternalNodes() {
Transaction tx = suspend();
try {
for (AddressAdapter member : currentView) {
CacheHelper.initInternalEvict(cacheAdapter, member);
}
} finally {
resume(tx);
}
}
public String getName() {
return name;
}
@ -94,13 +58,9 @@ public abstract class BaseRegion implements Region {
}
public long getElementCountInMemory() {
if (checkValid()) {
Set keySet = cacheAdapter.keySet();
int size = cacheAdapter.size();
if (CacheHelper.containsEvictAllNotification(keySet, address))
size--;
return size;
}
if (checkValid())
return cacheAdapter.size();
return 0;
}
@ -131,25 +91,15 @@ public abstract class BaseRegion implements Region {
}
public Map toMap() {
if (checkValid()) {
// If copying causes issues, provide a lazily loaded Map
Map map = new HashMap();
Set<Map.Entry> entries = cacheAdapter.toMap().entrySet();
for (Map.Entry entry : entries) {
Object key = entry.getKey();
if (!CacheHelper.isEvictAllNotification(key)) {
map.put(key, entry.getValue());
}
}
return map;
}
if (checkValid())
return cacheAdapter.toMap();
return Collections.EMPTY_MAP;
}
public void destroy() throws CacheException {
try {
cacheAdapter.stop();
// cacheAdapter.clear();
} finally {
cacheAdapter.removeListener(this);
}
@ -173,7 +123,15 @@ public abstract class BaseRegion implements Region {
if (invalidateState.compareAndSet(InvalidateState.INVALID, InvalidateState.CLEARING)) {
Transaction tx = suspend();
try {
cacheAdapter.withFlags(FlagAdapter.CACHE_MODE_LOCAL, FlagAdapter.ZERO_LOCK_ACQUISITION_TIMEOUT).clear();
// Clear region in a separate transaction
cacheAdapter.withinTx(new Callable<Void>() {
@Override
public Void call() throws Exception {
cacheAdapter.withFlags(FlagAdapter.CACHE_MODE_LOCAL,
FlagAdapter.ZERO_LOCK_ACQUISITION_TIMEOUT).clear();
return null;
}
});
invalidateState.compareAndSet(InvalidateState.CLEARING, InvalidateState.VALID);
}
catch (Exception e) {
@ -192,6 +150,8 @@ public abstract class BaseRegion implements Region {
return valid;
}
protected boolean isValid() {
return invalidateState.get() == InvalidateState.VALID;
}
@ -261,44 +221,13 @@ public abstract class BaseRegion implements Region {
}
}
@CacheEntryModified
public void entryModified(CacheEntryModifiedEvent event) {
handleEvictAllModification(event);
}
protected boolean handleEvictAllModification(CacheEntryModifiedEvent event) {
if (!event.isPre() && (replication || event.isOriginLocal()) && CacheHelper.isEvictAllNotification(event.getKey(), event.getValue())) {
if (log.isTraceEnabled()) log.tracef("Set invalid state because marker cache entry was put: {0}", event);
public void invalidateRegion() {
if (log.isTraceEnabled()) log.trace("Invalidate region: " + name);
invalidateState.set(InvalidateState.INVALID);
return true;
}
return false;
}
@CacheEntryInvalidated
public void entryInvalidated(CacheEntryInvalidatedEvent event) {
if (log.isTraceEnabled()) log.tracef("Cache entry invalidated: {0}", event);
handleEvictAllInvalidation(event);
}
protected boolean handleEvictAllInvalidation(CacheEntryInvalidatedEvent event) {
if (!event.isPre() && CacheHelper.isEvictAllNotification(event.getKey())) {
if (log.isTraceEnabled()) log.tracef("Set invalid state because marker cache entry was invalidated: {0}", event);
invalidateState.set(InvalidateState.INVALID);
return true;
}
return false;
}
@ViewChanged
public void viewChanged(ViewChangedEvent event) {
synchronized (currentView) {
List<AddressAdapter> view = AddressAdapterImpl.toAddressAdapter(event.getNewMembers());
if (view != null) {
currentView.addAll(view);
establishInternalNodes();
}
}
public TransactionManager getTransactionManager() {
return transactionManager;
}
}

View File

@ -1,4 +1,5 @@
package org.hibernate.cache.infinispan.impl;
import javax.transaction.TransactionManager;
import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cache.spi.RegionFactory;
@ -12,11 +13,14 @@ import org.hibernate.cache.infinispan.util.CacheAdapter;
* @author Galder Zamarreño
* @since 3.5
*/
public abstract class BaseTransactionalDataRegion extends BaseRegion implements TransactionalDataRegion {
public abstract class BaseTransactionalDataRegion
extends BaseRegion implements TransactionalDataRegion {
private final CacheDataDescription metadata;
public BaseTransactionalDataRegion(CacheAdapter cacheAdapter, String name, CacheDataDescription metadata, TransactionManager transactionManager, RegionFactory factory) {
public BaseTransactionalDataRegion(CacheAdapter cacheAdapter, String name,
CacheDataDescription metadata, TransactionManager transactionManager,
RegionFactory factory) {
super(cacheAdapter, name, transactionManager, factory);
this.metadata = metadata;
}

View File

@ -7,7 +7,6 @@ import org.hibernate.cache.spi.QueryResultsRegion;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.infinispan.impl.BaseTransactionalDataRegion;
import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cache.infinispan.util.CacheHelper;
import org.hibernate.cache.infinispan.util.FlagAdapter;
import org.infinispan.notifications.Listener;
@ -38,7 +37,7 @@ public class QueryResultsRegionImpl extends BaseTransactionalDataRegion implemen
public void evictAll() throws CacheException {
Transaction tx = suspend();
try {
CacheHelper.sendEvictAllNotification(cacheAdapter, getAddress());
cacheAdapter.broadcastEvictAll();
} finally {
resume(tx);
}

View File

@ -1,4 +1,5 @@
package org.hibernate.cache.infinispan.timestamp;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@ -9,12 +10,10 @@ import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.TimestampsRegion;
import org.hibernate.cache.infinispan.impl.BaseGeneralDataRegion;
import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cache.infinispan.util.CacheHelper;
import org.hibernate.cache.infinispan.util.FlagAdapter;
import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryModified;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved;
import org.infinispan.notifications.cachelistener.event.CacheEntryInvalidatedEvent;
import org.infinispan.notifications.cachelistener.event.CacheEntryModifiedEvent;
import org.infinispan.notifications.cachelistener.event.CacheEntryRemovedEvent;
@ -46,7 +45,7 @@ public class TimestampsRegionImpl extends BaseGeneralDataRegion implements Times
// TODO Is this a valid operation on a timestamps cache?
Transaction tx = suspend();
try {
CacheHelper.sendEvictAllNotification(cacheAdapter, getAddress());
cacheAdapter.broadcastEvictAll();
} finally {
resume(tx);
}
@ -74,18 +73,13 @@ public class TimestampsRegionImpl extends BaseGeneralDataRegion implements Times
return value;
}
public void put(Object key, Object value) throws CacheException {
// Don't hold the JBC node lock throughout the tx, as that
// prevents reads and other updates
Transaction tx = suspend();
public void put(final Object key, final Object value) throws CacheException {
try {
// We ensure ASYNC semantics (JBCACHE-1175) and make sure previous
// value is not loaded from cache store cos it's not needed.
cacheAdapter.withFlags(FlagAdapter.FORCE_ASYNCHRONOUS).put(key, value);
} catch (Exception e) {
throw new CacheException(e);
} finally {
resume(tx);
}
}
@ -103,10 +97,9 @@ public class TimestampsRegionImpl extends BaseGeneralDataRegion implements Times
*/
@CacheEntryModified
public void nodeModified(CacheEntryModifiedEvent event) {
if (!handleEvictAllModification(event) && !event.isPre()) {
if (!event.isPre())
localCache.put(event.getKey(), event.getValue());
}
}
/**
* Monitors cache events and updates the local cache
@ -120,22 +113,10 @@ public class TimestampsRegionImpl extends BaseGeneralDataRegion implements Times
}
@Override
protected boolean handleEvictAllModification(CacheEntryModifiedEvent event) {
boolean result = super.handleEvictAllModification(event);
if (result) {
public void invalidateRegion() {
super.invalidateRegion(); // Invalidate first
localCache.clear();
}
return result;
}
@Override
protected boolean handleEvictAllInvalidation(CacheEntryInvalidatedEvent event) {
boolean result = super.handleEvictAllInvalidation(event);
if (result) {
localCache.clear();
}
return result;
}
/**
* Brings all data from the distributed cache into our local cache.

View File

@ -24,6 +24,8 @@ package org.hibernate.cache.infinispan.util;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import org.hibernate.cache.CacheException;
import org.infinispan.Cache;
import org.infinispan.config.Configuration;
@ -204,4 +206,21 @@ public interface CacheAdapter {
* @return Configuration instance associated with this cache.
*/
Configuration getConfiguration();
/**
* TODO
*/
void broadcastEvictAll();
/**
* TODO
*
* @param c
* @param <T>
* @return
*/
<T> T withinTx(Callable<T> c) throws Exception;
Cache getCache();
}

View File

@ -21,10 +21,14 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.hibernate.cache.infinispan.util;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import org.hibernate.cache.CacheException;
import org.infinispan.AdvancedCache;
import org.infinispan.Cache;
import org.infinispan.config.Configuration;
import org.infinispan.context.Flag;
@ -42,13 +46,18 @@ import org.infinispan.util.logging.LogFactory;
public class CacheAdapterImpl implements CacheAdapter {
private static final Log log = LogFactory.getLog(CacheAdapterImpl.class);
private final Cache cache;
private final AdvancedCache cache;
private final CacheCommandInitializer cacheCmdInitializer;
private final boolean isSync;
private CacheAdapterImpl(Cache cache) {
private CacheAdapterImpl(AdvancedCache cache) {
this.cache = cache;
this.cacheCmdInitializer = cache.getComponentRegistry()
.getComponent(CacheCommandInitializer.class);
this.isSync = isSynchronous(cache.getConfiguration().getCacheMode());
}
public static CacheAdapter newInstance(Cache cache) {
public static CacheAdapter newInstance(AdvancedCache cache) {
return new CacheAdapterImpl(cache);
}
@ -61,7 +70,7 @@ public class CacheAdapterImpl implements CacheAdapter {
}
public boolean isSynchronous() {
return isSynchronous(cache.getConfiguration().getCacheMode());
return isSync;
}
public Set keySet() {
@ -70,7 +79,7 @@ public class CacheAdapterImpl implements CacheAdapter {
public CacheAdapter withFlags(FlagAdapter... flagAdapters) {
Flag[] flags = FlagAdapter.toFlags(flagAdapters);
return newInstance(cache.getAdvancedCache().withFlags(flags));
return newInstance(cache.withFlags(flags));
}
public Object get(Object key) throws CacheException {
@ -173,7 +182,7 @@ public class CacheAdapterImpl implements CacheAdapter {
}
public AddressAdapter getAddress() {
RpcManager rpc = cache.getAdvancedCache().getRpcManager();
RpcManager rpc = cache.getRpcManager();
if (rpc != null) {
return AddressAdapterImpl.newInstance(rpc.getTransport().getAddress());
}
@ -181,17 +190,13 @@ public class CacheAdapterImpl implements CacheAdapter {
}
public List<AddressAdapter> getMembers() {
RpcManager rpc = cache.getAdvancedCache().getRpcManager();
RpcManager rpc = cache.getRpcManager();
if (rpc != null) {
return AddressAdapterImpl.toAddressAdapter(rpc.getTransport().getMembers());
}
return null;
}
public RpcManager getRpcManager() {
return cache.getAdvancedCache().getRpcManager();
}
public int size() {
return cache.size();
}
@ -212,17 +217,33 @@ public class CacheAdapterImpl implements CacheAdapter {
return cache.getConfiguration();
}
@Override
public void broadcastEvictAll() {
EvictAllCommand cmd = cacheCmdInitializer.buildEvictAllCommand(cache.getName());
cache.getRpcManager().broadcastRpcCommand(cmd, isSync);
}
@Override
public <T> T withinTx(Callable<T> c) throws Exception {
return CacheHelper.withinTx(cache.getTransactionManager(), c);
}
@Override
public Cache getCache() {
return cache;
}
private Cache getFailSilentCache() {
return cache.getAdvancedCache().withFlags(Flag.FAIL_SILENTLY);
return cache.withFlags(Flag.FAIL_SILENTLY);
}
private Cache getSkipRemoteGetLoadCache() {
return cache.getAdvancedCache().withFlags(
return cache.withFlags(
Flag.SKIP_CACHE_LOAD, Flag.SKIP_REMOTE_LOOKUP);
}
private Cache getFailSilentCacheSkipRemotes() {
return cache.getAdvancedCache().withFlags(
return cache.withFlags(
Flag.FAIL_SILENTLY, Flag.SKIP_CACHE_LOAD, Flag.SKIP_REMOTE_LOOKUP);
}

View File

@ -0,0 +1,28 @@
package org.hibernate.cache.infinispan.util;
import org.infinispan.commands.module.ExtendedModuleCommandFactory;
import org.infinispan.commands.module.ModuleCommandExtensions;
import org.infinispan.commands.module.ModuleCommandInitializer;
/**
* Command extensions for second-level cache use case
*
* @author Galder Zamarreño
* @since 4.0
*/
public class CacheCommandExtensions implements ModuleCommandExtensions {
final CacheCommandFactory cacheCommandFactory = new CacheCommandFactory();
final CacheCommandInitializer cacheCommandInitializer = new CacheCommandInitializer();
@Override
public ExtendedModuleCommandFactory getModuleCommandFactory() {
return cacheCommandFactory;
}
@Override
public ModuleCommandInitializer getModuleCommandInitializer() {
return cacheCommandInitializer;
}
}

View File

@ -0,0 +1,53 @@
package org.hibernate.cache.infinispan.util;
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.infinispan.commands.ReplicableCommand;
import org.infinispan.commands.module.ExtendedModuleCommandFactory;
import org.infinispan.commands.remote.CacheRpcCommand;
import java.util.HashMap;
import java.util.Map;
/**
* Command factory
*
* @author Galder Zamarreño
* @since 4.0
*/
public class CacheCommandFactory implements ExtendedModuleCommandFactory {
private InfinispanRegionFactory regionFactory;
public void setRegionFactory(InfinispanRegionFactory regionFactory) {
this.regionFactory = regionFactory;
}
@Override
public Map<Byte, Class<? extends ReplicableCommand>> getModuleCommands() {
Map<Byte, Class<? extends ReplicableCommand>> map = new HashMap<Byte, Class<? extends ReplicableCommand>>(3);
map.put(CacheCommandIds.EVICT_ALL, EvictAllCommand.class);
return map;
}
@Override
public CacheRpcCommand fromStream(byte commandId, Object[] args, String cacheName) {
CacheRpcCommand c;
switch (commandId) {
case CacheCommandIds.EVICT_ALL:
c = new EvictAllCommand(cacheName, regionFactory);
break;
default:
throw new IllegalArgumentException("Not registered to handle command id " + commandId);
}
c.setParameters(commandId, args);
return c;
}
@Override
public ReplicableCommand fromStream(byte commandId, Object[] args) {
// Should not be called while this factory only
// provides cache specific replicable commands.
return null;
}
}

View File

@ -0,0 +1,13 @@
package org.hibernate.cache.infinispan.util;
/**
* Command id range assigned to Hibernate second level cache: 120 - 139
*
* @author Galder Zamarreño
* @since 4.0
*/
public interface CacheCommandIds {
byte EVICT_ALL = 120;
}

View File

@ -0,0 +1,30 @@
package org.hibernate.cache.infinispan.util;
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.infinispan.commands.ReplicableCommand;
import org.infinispan.commands.module.ModuleCommandInitializer;
/**
* Command initializer
*
* @author Galder Zamarreño
* @since 4.0
*/
public class CacheCommandInitializer implements ModuleCommandInitializer {
private InfinispanRegionFactory regionFactory;
public void setRegionFactory(InfinispanRegionFactory regionFactory) {
this.regionFactory = regionFactory;
}
public EvictAllCommand buildEvictAllCommand(String regionName) {
return new EvictAllCommand(regionName, regionFactory);
}
@Override
public void initializeReplicableCommand(ReplicableCommand c, boolean isRemote) {
// No need to initialize...
}
}

View File

@ -22,13 +22,10 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.cache.infinispan.util;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Set;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import java.util.concurrent.Callable;
import javax.transaction.Status;
import javax.transaction.TransactionManager;
/**
* Helper for dealing with Infinisan cache instances.
@ -38,80 +35,23 @@ import org.infinispan.util.logging.LogFactory;
*/
public class CacheHelper {
private static final Log log = LogFactory.getLog(CacheHelper.class);
/**
* Disallow external instantiation of CacheHelper.
*/
private CacheHelper() {
}
public static void initInternalEvict(CacheAdapter cache, AddressAdapter member) {
EvictAll eKey = new EvictAll(member == null ? NoAddress.INSTANCE : member);
cache.withFlags(FlagAdapter.CACHE_MODE_LOCAL).put(eKey, Internal.INIT);
public static <T> T withinTx(TransactionManager tm, Callable<T> c) throws Exception {
tm.begin();
try {
return c.call();
} catch (Exception e) {
tm.setRollbackOnly();
throw e;
} finally {
if (tm.getStatus() == Status.STATUS_ACTIVE) tm.commit();
else tm.rollback();
}
public static void sendEvictAllNotification(CacheAdapter cache, AddressAdapter member) {
EvictAll eKey = new EvictAll(member == null ? NoAddress.INSTANCE : member);
cache.put(eKey, Internal.EVICT);
}
public static boolean isEvictAllNotification(Object key) {
return key instanceof EvictAll;
}
public static boolean containsEvictAllNotification(Set keySet, AddressAdapter member) {
EvictAll eKey = new EvictAll(member == null ? NoAddress.INSTANCE : member);
return keySet.contains(eKey);
}
public static boolean isEvictAllNotification(Object key, Object value) {
return key instanceof EvictAll && value == Internal.EVICT;
}
public static class EvictAll implements Externalizable {
AddressAdapter member;
// Required by Java Externalizable
public EvictAll() {
}
public EvictAll(AddressAdapter member) {
this.member = member;
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof EvictAll))
return false;
EvictAll ek = (EvictAll) obj;
return ek.member.equals(member);
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + member.hashCode();
return result;
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
member = (AddressAdapter) in.readObject();
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(member);
}
}
private enum NoAddress implements AddressAdapter {
INSTANCE;
}
private enum Internal {
INIT, EVICT;
}
}

View File

@ -0,0 +1,50 @@
package org.hibernate.cache.infinispan.util;
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cache.infinispan.impl.BaseRegion;
import org.infinispan.commands.remote.BaseRpcCommand;
import org.infinispan.context.InvocationContext;
/**
* Evict all command
*
* @author Galder Zamarreño
* @since 4.0
*/
public class EvictAllCommand extends BaseRpcCommand {
private InfinispanRegionFactory regionFactory;
public EvictAllCommand(String regionName, InfinispanRegionFactory regionFactory) {
super(regionName); // region name and cache names are the same...
this.regionFactory = regionFactory;
}
@Override
public Object perform(InvocationContext ctx) throws Throwable {
BaseRegion region = regionFactory.getRegion(cacheName);
region.invalidateRegion();
return null;
}
@Override
public byte getCommandId() {
return CacheCommandIds.EVICT_ALL;
}
@Override
public Object[] getParameters() {
return new Object[0];
}
@Override
public void setParameters(int commandId, Object[] parameters) {
// No-op
}
@Override
public boolean isReturnValueExpected() {
return false;
}
}

View File

@ -0,0 +1 @@
org.hibernate.cache.infinispan.util.CacheCommandExtensions

View File

@ -6,11 +6,13 @@
<transport
transportClass="org.infinispan.remoting.transport.jgroups.JGroupsTransport"
clusterName="infinispan-hibernate-cluster"
distributedSyncTimeout="50000">
distributedSyncTimeout="50000"
strictPeerToPeer="false">
<!-- Note that the JGroups transport uses sensible defaults if no configuration property is defined. -->
<properties>
<!-- TODO: Change to udp.xml once streaming transfer requirement has been removed. -->
<property name="configurationFile" value="flush-udp.xml"/>
<!--<property name="configurationFile" value="jgroups-tcp.xml"/>-->
<property name="configurationFile"
value="${hibernate.cache.infinispan.jgroups_cfg:tcp.xml}"/>
</properties>
<!-- See the JGroupsTransport javadocs for more flags -->
</transport>
@ -19,11 +21,6 @@
<default>
<!-- Used to register JMX statistics in any available MBean server -->
<jmxStatistics enabled="false"/>
<!-- autoCommit would be better configured to be false
but that would require redesigning evictAll handling -->
<transaction autoCommit="true" use1PcForAutoCommitTransactions="false"
transactionManagerLookupClass="org.hibernate.cache.infinispan.tm.HibernateTransactionManagerLookup"
lockingMode="OPTIMISTIC"/>
</default>
<!-- Default configuration is appropriate for entity/collection caching. -->
@ -36,9 +33,11 @@
lockAcquisitionTimeout="15000" useLockStriping="false"/>
<!-- Eviction configuration. WakeupInterval defines how often the eviction thread runs, in milliseconds.
0 means the eviction thread will never run. A separate executor is used for eviction in each cache. -->
<eviction wakeUpInterval="5000" maxEntries="10000" strategy="LRU"/>
<expiration maxIdle="100000"/>
<eviction maxEntries="10000" strategy="LRU"/>
<expiration maxIdle="100000" wakeUpInterval="5000"/>
<lazyDeserialization enabled="true"/>
<transaction transactionMode="TRANSACTIONAL" autoCommit="false"
lockingMode="OPTIMISTIC"/>
</namedCache>
<!-- Default configuration is appropriate for entity/collection caching. -->
@ -57,9 +56,11 @@
lockAcquisitionTimeout="15000" useLockStriping="false"/>
<!-- Eviction configuration. WakeupInterval defines how often the eviction thread runs, in milliseconds.
0 means the eviction thread will never run. A separate executor is used for eviction in each cache. -->
<eviction wakeUpInterval="5000" maxEntries="10000" strategy="LRU"/>
<expiration maxIdle="100000"/>
<eviction maxEntries="10000" strategy="LRU"/>
<expiration maxIdle="100000" wakeUpInterval="5000"/>
<lazyDeserialization enabled="true"/>
<transaction transactionMode="TRANSACTIONAL" autoCommit="false"
lockingMode="OPTIMISTIC"/>
</namedCache>
<!-- An alternative configuration for entity/collection caching that uses replication instead of invalidation -->
@ -72,9 +73,11 @@
lockAcquisitionTimeout="15000" useLockStriping="false"/>
<!-- Eviction configuration. WakeupInterval defines how often the eviction thread runs, in milliseconds.
0 means the eviction thread will never run. A separate executor is used for eviction in each cache. -->
<eviction wakeUpInterval="5000" maxEntries="10000" strategy="LRU"/>
<expiration maxIdle="100000"/>
<eviction maxEntries="10000" strategy="LRU"/>
<expiration maxIdle="100000" wakeUpInterval="5000"/>
<lazyDeserialization enabled="true"/>
<transaction transactionMode="TRANSACTIONAL" autoCommit="false"
lockingMode="OPTIMISTIC"/>
</namedCache>
<!-- A config appropriate for query caching. Does not replicate queries. -->
@ -83,8 +86,10 @@
lockAcquisitionTimeout="15000" useLockStriping="false"/>
<!--Eviction configuration. WakeupInterval defines how often the eviction thread runs, in milliseconds. 0 means
the eviction thread will never run. A separate executor is used for eviction in each cache. -->
<eviction wakeUpInterval="5000" maxEntries="10000" strategy="LRU"/>
<expiration maxIdle="100000"/>
<eviction maxEntries="10000" strategy="LRU"/>
<expiration maxIdle="100000" wakeUpInterval="5000"/>
<transaction transactionMode="TRANSACTIONAL" autoCommit="false"
lockingMode="OPTIMISTIC"/>
</namedCache>
<!-- A query cache that replicates queries. Replication is asynchronous. -->
@ -96,8 +101,10 @@
lockAcquisitionTimeout="15000" useLockStriping="false"/>
<!--Eviction configuration. WakeupInterval defines how often the eviction thread runs, in milliseconds. 0 means
the eviction thread will never run. A separate executor is used for eviction in each cache. -->
<eviction wakeUpInterval="5000" maxEntries="10000" strategy="LRU"/>
<expiration maxIdle="100000"/>
<eviction maxEntries="10000" strategy="LRU"/>
<expiration maxIdle="100000" wakeUpInterval="5000"/>
<transaction transactionMode="TRANSACTIONAL" autoCommit="false"
lockingMode="OPTIMISTIC"/>
<!-- State transfer forces all replication calls to be synchronous,
so for calls to remain async, use a cluster cache loader instead -->
<loaders passivation="false" shared="false" preload="false">
@ -122,7 +129,10 @@
lockAcquisitionTimeout="15000" useLockStriping="false"/>
<lazyDeserialization enabled="true"/>
<!-- Don't ever evict modification timestamps -->
<eviction wakeUpInterval="0" strategy="NONE"/>
<eviction strategy="NONE"/>
<expiration wakeUpInterval="0"/>
<!-- Explicitly non transactional -->
<transaction transactionMode="NON_TRANSACTIONAL"/>
<!-- State transfer forces all replication calls to be synchronous,
so for calls to remain async, use a cluster cache loader instead -->
<loaders passivation="false" shared="false" preload="false">

View File

@ -110,7 +110,7 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm
assertNull( "local is clean", localRegion.get( KEY ) );
assertNull( "remote is clean", remoteRegion.get( KEY ) );
localRegion.put( KEY, VALUE1 );
regionPut(localRegion);
assertEquals( VALUE1, localRegion.get( KEY ) );
// allow async propagation
@ -118,7 +118,7 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm
Object expected = invalidation ? null : VALUE1;
assertEquals( expected, remoteRegion.get( KEY ) );
localRegion.evict( KEY );
regionEvict(localRegion);
// allow async propagation
sleep( 250 );
@ -126,6 +126,14 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm
assertEquals( null, remoteRegion.get( KEY ) );
}
protected void regionEvict(GeneralDataRegion region) throws Exception {
region.evict(KEY);
}
protected void regionPut(GeneralDataRegion region) throws Exception {
region.put(KEY, VALUE1);
}
protected abstract String getStandardRegionName(String regionPrefix);
/**
@ -184,13 +192,13 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm
assertNull( "local is clean", localRegion.get( KEY ) );
assertNull( "remote is clean", remoteRegion.get( KEY ) );
localRegion.put( KEY, VALUE1 );
regionPut(localRegion);
assertEquals( VALUE1, localRegion.get( KEY ) );
// Allow async propagation
sleep( 250 );
remoteRegion.put( KEY, VALUE1 );
regionPut(remoteRegion);
assertEquals( VALUE1, remoteRegion.get( KEY ) );
// Allow async propagation

View File

@ -29,7 +29,6 @@ import org.infinispan.Cache;
import org.jboss.logging.Logger;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.infinispan.util.CacheHelper;
import org.junit.After;
import org.junit.Before;
@ -43,19 +42,23 @@ import org.hibernate.test.cache.infinispan.util.CacheTestSupport;
* @since 3.5
*/
public abstract class AbstractNonFunctionalTestCase extends org.hibernate.testing.junit4.BaseUnitTestCase {
private static final Logger log = Logger.getLogger( AbstractNonFunctionalTestCase.class );
private static final Logger log = Logger.getLogger(AbstractNonFunctionalTestCase.class);
public static final String REGION_PREFIX = "test";
private static final String PREFER_IPV4STACK = "java.net.preferIPv4Stack";
private String preferIPv4Stack;
private static final String JGROUPS_CFG_FILE = "hibernate.cache.infinispan.jgroups_cfg";
private String jgroupsCfgFile;
private CacheTestSupport testSupport = new CacheTestSupport();
@Before
public void prepareCacheSupport() throws Exception {
preferIPv4Stack = System.getProperty( PREFER_IPV4STACK );
System.setProperty( PREFER_IPV4STACK, "true" );
preferIPv4Stack = System.getProperty(PREFER_IPV4STACK);
System.setProperty(PREFER_IPV4STACK, "true");
jgroupsCfgFile = System.getProperty(JGROUPS_CFG_FILE);
System.setProperty(JGROUPS_CFG_FILE, "stacks/tcp.xml");
testSupport.setUp();
}
@ -64,12 +67,16 @@ public abstract class AbstractNonFunctionalTestCase extends org.hibernate.testin
public void releaseCachSupport() throws Exception {
testSupport.tearDown();
if ( preferIPv4Stack == null ) {
System.clearProperty( PREFER_IPV4STACK );
}
else {
System.setProperty( PREFER_IPV4STACK, preferIPv4Stack );
if (preferIPv4Stack == null) {
System.clearProperty(PREFER_IPV4STACK);
} else {
System.setProperty(PREFER_IPV4STACK, preferIPv4Stack);
}
if (jgroupsCfgFile == null)
System.clearProperty(JGROUPS_CFG_FILE);
else
System.setProperty(JGROUPS_CFG_FILE, jgroupsCfgFile);
}
protected void registerCache(Cache cache) {
@ -95,8 +102,7 @@ public abstract class AbstractNonFunctionalTestCase extends org.hibernate.testin
protected void sleep(long ms) {
try {
Thread.sleep(ms);
}
catch (InterruptedException e) {
} catch (InterruptedException e) {
log.warn("Interrupted during sleep", e);
}
}
@ -106,13 +112,7 @@ public abstract class AbstractNonFunctionalTestCase extends org.hibernate.testin
}
protected int getValidKeyCount(Set keys) {
int result = 0;
for (Object key : keys) {
if (!(CacheHelper.isEvictAllNotification(key))) {
result++;
}
}
return result;
return keys.size();
}
}

View File

@ -31,10 +31,8 @@ import org.hibernate.cache.infinispan.timestamp.TimestampsRegionImpl;
import org.hibernate.cache.infinispan.tm.HibernateTransactionManagerLookup;
import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cfg.Settings;
import org.hibernate.cfg.SettingsFactory;
import org.hibernate.service.jta.platform.internal.AbstractJtaPlatform;
import org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform;
import org.hibernate.service.jta.platform.spi.JtaPlatform;
import org.hibernate.testing.ServiceRegistryBuilder;
import org.infinispan.config.Configuration;
import org.infinispan.config.Configuration.CacheMode;
@ -43,13 +41,6 @@ import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
import org.junit.Test;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.InvalidTransactionException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
/**
@ -78,8 +69,7 @@ public class InfinispanRegionFactoryTestCase {
p.setProperty("hibernate.cache.infinispan.query.eviction.wake_up_interval", "3000");
p.setProperty("hibernate.cache.infinispan.query.eviction.max_entries", "10000");
InfinispanRegionFactory factory = new InfinispanRegionFactory();
factory.start(null, p);
InfinispanRegionFactory factory = createRegionFactory(p);
assertEquals("entity", factory.getTypeOverrides().get("entity").getCacheName());
assertEquals("entity", factory.getTypeOverrides().get("collection").getCacheName());
@ -101,6 +91,7 @@ public class InfinispanRegionFactoryTestCase {
assertEquals(3000, factory.getTypeOverrides().get("query").getEvictionWakeUpInterval());
assertEquals(10000, factory.getTypeOverrides().get("query").getEvictionMaxEntries());
}
@Test
public void testBuildEntityCollectionRegionsPersonPlusEntityCollectionOverrides() {
final String person = "com.acme.Person";
@ -130,8 +121,7 @@ public class InfinispanRegionFactoryTestCase {
p.setProperty("hibernate.cache.infinispan.collection.eviction.strategy", "LRU");
p.setProperty("hibernate.cache.infinispan.collection.eviction.wake_up_interval", "3500");
p.setProperty("hibernate.cache.infinispan.collection.eviction.max_entries", "25000");
InfinispanRegionFactory factory = createRegionFactory();
factory.start(null, p);
InfinispanRegionFactory factory = createRegionFactory(p);
EmbeddedCacheManager manager = factory.getCacheManager();
try {
assertFalse(manager.getGlobalConfiguration().isExposeGlobalJmxStatistics());
@ -217,7 +207,7 @@ public class InfinispanRegionFactoryTestCase {
@Test
public void testBuildEntityCollectionRegionOverridesOnly() {
CacheAdapter cache = null;
CacheAdapter cache;
Properties p = new Properties();
p.setProperty("hibernate.cache.infinispan.entity.eviction.strategy", "FIFO");
p.setProperty("hibernate.cache.infinispan.entity.eviction.wake_up_interval", "3000");
@ -225,8 +215,7 @@ public class InfinispanRegionFactoryTestCase {
p.setProperty("hibernate.cache.infinispan.collection.eviction.strategy", "LRU");
p.setProperty("hibernate.cache.infinispan.collection.eviction.wake_up_interval", "3500");
p.setProperty("hibernate.cache.infinispan.collection.eviction.max_entries", "35000");
InfinispanRegionFactory factory = createRegionFactory();
factory.start(null, p);
InfinispanRegionFactory factory = createRegionFactory(p);
factory.getCacheManager();
try {
EntityRegionImpl region = (EntityRegionImpl) factory.buildEntityRegion("com.acme.Address", p, null);
@ -262,8 +251,7 @@ public class InfinispanRegionFactoryTestCase {
p.setProperty("hibernate.cache.infinispan.entity.eviction.strategy", "FIFO");
p.setProperty("hibernate.cache.infinispan.entity.eviction.wake_up_interval", "3000");
p.setProperty("hibernate.cache.infinispan.entity.eviction.max_entries", "10000");
InfinispanRegionFactory factory = createRegionFactory();
factory.start(null, p);
InfinispanRegionFactory factory = createRegionFactory(p);
EmbeddedCacheManager manager = factory.getCacheManager();
try {
assertNotNull(factory.getTypeOverrides().get(person));
@ -286,7 +274,7 @@ public class InfinispanRegionFactoryTestCase {
public void testTimestampValidation() {
Properties p = new Properties();
final DefaultCacheManager manager = new DefaultCacheManager();
InfinispanRegionFactory factory = createRegionFactory(manager);
InfinispanRegionFactory factory = createRegionFactory(manager, p);
Configuration config = new Configuration();
config.setCacheMode(CacheMode.INVALIDATION_SYNC);
manager.defineConfiguration("timestamps", config);
@ -300,8 +288,7 @@ public class InfinispanRegionFactoryTestCase {
public void testBuildDefaultTimestampsRegion() {
final String timestamps = "org.hibernate.cache.spi.UpdateTimestampsCache";
Properties p = new Properties();
InfinispanRegionFactory factory = createRegionFactory();
factory.start(null, p);
InfinispanRegionFactory factory = createRegionFactory(p);
EmbeddedCacheManager manager = factory.getCacheManager();
try {
assertTrue(factory.getDefinedConfigurations().contains("timestamps"));
@ -325,8 +312,7 @@ public class InfinispanRegionFactoryTestCase {
final String timestamps = "org.hibernate.cache.spi.UpdateTimestampsCache";
Properties p = new Properties();
p.setProperty("hibernate.cache.infinispan.timestamps.cfg", "unrecommended-timestamps");
InfinispanRegionFactory factory = createRegionFactory();
factory.start(null, p);
InfinispanRegionFactory factory = createRegionFactory(p);
EmbeddedCacheManager manager = factory.getCacheManager();
try {
assertFalse(factory.getDefinedConfigurations().contains("timestamp"));
@ -351,9 +337,8 @@ public class InfinispanRegionFactoryTestCase {
public void testBuildTimestamRegionWithCacheNameOverride() {
final String timestamps = "org.hibernate.cache.spi.UpdateTimestampsCache";
Properties p = new Properties();
InfinispanRegionFactory factory = createRegionFactory();
p.setProperty("hibernate.cache.infinispan.timestamps.cfg", "mytimestamps-cache");
factory.start(null, p);
InfinispanRegionFactory factory = createRegionFactory(p);
try {
factory.buildTimestampsRegion(timestamps, p);
assertTrue(factory.getDefinedConfigurations().contains("mytimestamps-cache"));
@ -365,31 +350,30 @@ public class InfinispanRegionFactoryTestCase {
public void testBuildTimestamRegionWithFifoEvictionOverride() {
final String timestamps = "org.hibernate.cache.spi.UpdateTimestampsCache";
Properties p = new Properties();
InfinispanRegionFactory factory = createRegionFactory();
p.setProperty("hibernate.cache.infinispan.timestamps.cfg", "mytimestamps-cache");
p.setProperty("hibernate.cache.infinispan.timestamps.eviction.strategy", "FIFO");
p.setProperty("hibernate.cache.infinispan.timestamps.eviction.wake_up_interval", "3000");
p.setProperty("hibernate.cache.infinispan.timestamps.eviction.max_entries", "10000");
InfinispanRegionFactory factory = null;
try {
factory.start(null, p);
factory = createRegionFactory(p);
factory.buildTimestampsRegion(timestamps, p);
assertTrue(factory.getDefinedConfigurations().contains("mytimestamps-cache"));
fail("Should fail cos no eviction configurations are allowed for timestamp caches");
} catch(CacheException ce) {
} finally {
factory.stop();
if (factory != null) factory.stop();
}
}
@Test
public void testBuildTimestamRegionWithNoneEvictionOverride() {
final String timestamps = "org.hibernate.cache.spi.UpdateTimestampsCache";
Properties p = new Properties();
InfinispanRegionFactory factory = createRegionFactory();
p.setProperty("hibernate.cache.infinispan.timestamps.cfg", "timestamps-none-eviction");
p.setProperty("hibernate.cache.infinispan.timestamps.eviction.strategy", "NONE");
p.setProperty("hibernate.cache.infinispan.timestamps.eviction.wake_up_interval", "3000");
p.setProperty("hibernate.cache.infinispan.timestamps.eviction.max_entries", "10000");
factory.start(null, p);
InfinispanRegionFactory factory = createRegionFactory(p);
EmbeddedCacheManager manager = factory.getCacheManager();
manager.getGlobalConfiguration().setTransportClass(null);
try {
@ -403,8 +387,7 @@ public class InfinispanRegionFactoryTestCase {
public void testBuildQueryRegion() {
final String query = "org.hibernate.cache.internal.StandardQueryCache";
Properties p = new Properties();
InfinispanRegionFactory factory = createRegionFactory();
factory.start(null, p);
InfinispanRegionFactory factory = createRegionFactory(p);
EmbeddedCacheManager manager = factory.getCacheManager();
manager.getGlobalConfiguration().setTransportClass(null);
try {
@ -422,12 +405,11 @@ public class InfinispanRegionFactoryTestCase {
public void testBuildQueryRegionWithCustomRegionName() {
final String queryRegionName = "myquery";
Properties p = new Properties();
InfinispanRegionFactory factory = createRegionFactory();
p.setProperty("hibernate.cache.infinispan.myquery.cfg", "timestamps-none-eviction");
p.setProperty("hibernate.cache.infinispan.myquery.eviction.strategy", "FIFO");
p.setProperty("hibernate.cache.infinispan.myquery.eviction.wake_up_interval", "2222");
p.setProperty("hibernate.cache.infinispan.myquery.eviction.max_entries", "11111");
factory.start(null, p);
InfinispanRegionFactory factory = createRegionFactory(p);
EmbeddedCacheManager manager = factory.getCacheManager();
manager.getGlobalConfiguration().setTransportClass(null);
try {
@ -454,8 +436,7 @@ public class InfinispanRegionFactoryTestCase {
p.setProperty("hibernate.cache.infinispan.entity.eviction.strategy", "FIFO");
p.setProperty("hibernate.cache.infinispan.entity.eviction.wake_up_interval", "3000");
p.setProperty("hibernate.cache.infinispan.entity.eviction.max_entries", "10000");
InfinispanRegionFactory factory = createRegionFactory();
factory.start(null, p);
InfinispanRegionFactory factory = createRegionFactory(p);
EmbeddedCacheManager manager = factory.getCacheManager();
try {
assertTrue(manager.getGlobalConfiguration().isExposeGlobalJmxStatistics());
@ -502,8 +483,7 @@ public class InfinispanRegionFactoryTestCase {
p.setProperty("hibernate.cache.infinispan.entity.eviction.strategy", "FIFO");
p.setProperty("hibernate.cache.infinispan.entity.eviction.wake_up_interval", "3000");
p.setProperty("hibernate.cache.infinispan.entity.eviction.max_entries", "10000");
InfinispanRegionFactory factory = createRegionFactory();
factory.start(null, p);
InfinispanRegionFactory factory = createRegionFactory(p);
EmbeddedCacheManager manager = factory.getCacheManager();
try {
assertFalse(manager.getGlobalConfiguration().isExposeGlobalJmxStatistics());
@ -541,12 +521,12 @@ public class InfinispanRegionFactoryTestCase {
}
}
private InfinispanRegionFactory createRegionFactory() {
return createRegionFactory(null);
private InfinispanRegionFactory createRegionFactory(Properties p) {
return createRegionFactory(null, p);
}
private InfinispanRegionFactory createRegionFactory(final EmbeddedCacheManager manager) {
return new InfinispanRegionFactory() {
private InfinispanRegionFactory createRegionFactory(final EmbeddedCacheManager manager, Properties p) {
final InfinispanRegionFactory factory = new InfinispanRegionFactory() {
@Override
protected HibernateTransactionManagerLookup createTransactionManagerLookup(Settings settings, Properties properties) {
return new HibernateTransactionManagerLookup(null, null) {
@ -567,6 +547,8 @@ public class InfinispanRegionFactoryTestCase {
return super.createCacheManager(properties);
}
};
factory.start(null, p);
return factory;
}
}

View File

@ -25,6 +25,7 @@ package org.hibernate.test.cache.infinispan;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
@ -37,6 +38,8 @@ import org.hibernate.service.internal.StandardServiceRegistryImpl;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import static org.hibernate.cache.infinispan.util.CacheHelper.withinTx;
/**
* Defines the environment for a node.
*
@ -117,15 +120,27 @@ public class NodeEnvironment {
public void release() throws Exception {
if ( entityRegionMap != null ) {
for ( EntityRegionImpl region : entityRegionMap.values() ) {
region.getCacheAdapter().withFlags( FlagAdapter.CACHE_MODE_LOCAL ).clear();
for ( final EntityRegionImpl region : entityRegionMap.values() ) {
withinTx(region.getTransactionManager(), new Callable<Void>() {
@Override
public Void call() throws Exception {
region.getCacheAdapter().withFlags(FlagAdapter.CACHE_MODE_LOCAL).clear();
return null;
}
});
region.getCacheAdapter().stop();
}
entityRegionMap.clear();
}
if ( collectionRegionMap != null ) {
for ( CollectionRegionImpl collectionRegion : collectionRegionMap.values() ) {
for ( final CollectionRegionImpl collectionRegion : collectionRegionMap.values() ) {
withinTx(collectionRegion.getTransactionManager(), new Callable<Void>() {
@Override
public Void call() throws Exception {
collectionRegion.getCacheAdapter().withFlags( FlagAdapter.CACHE_MODE_LOCAL ).clear();
return null;
}
});
collectionRegion.getCacheAdapter().stop();
}
collectionRegionMap.clear();

View File

@ -31,6 +31,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.hibernate.cache.infinispan.util.CacheHelper;
import org.infinispan.transaction.tm.BatchModeTransactionManager;
import org.jboss.logging.Logger;
@ -52,7 +53,6 @@ import junit.framework.AssertionFailedError;
import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase;
import org.hibernate.test.cache.infinispan.NodeEnvironment;
import org.hibernate.test.cache.infinispan.functional.cluster.DualNodeJtaTransactionManagerImpl;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import static org.junit.Assert.assertEquals;
@ -162,8 +162,8 @@ public abstract class AbstractCollectionRegionAccessStrategyTestCase extends Abs
public void testPutFromLoadRemoveDoesNotProduceStaleData() throws Exception {
final CountDownLatch pferLatch = new CountDownLatch( 1 );
final CountDownLatch removeLatch = new CountDownLatch( 1 );
TransactionManager tm = DualNodeJtaTransactionManagerImpl.getInstance( "test1234" );
PutFromLoadValidator validator = new PutFromLoadValidator( tm ) {
final TransactionManager remoteTm = remoteCollectionRegion.getTransactionManager();
PutFromLoadValidator validator = new PutFromLoadValidator(remoteTm) {
@Override
public boolean acquirePutFromLoadLock(Object key) {
boolean acquired = super.acquirePutFromLoadLock( key );
@ -182,9 +182,10 @@ public abstract class AbstractCollectionRegionAccessStrategyTestCase extends Abs
return acquired;
}
};
final TransactionalAccessDelegate delegate = new TransactionalAccessDelegate(
(CollectionRegionImpl) localCollectionRegion, validator
);
final TransactionalAccessDelegate delegate =
new TransactionalAccessDelegate(localCollectionRegion, validator);
final TransactionManager localTm = localCollectionRegion.getTransactionManager();
Callable<Void> pferCallable = new Callable<Void>() {
public Void call() throws Exception {
@ -196,7 +197,13 @@ public abstract class AbstractCollectionRegionAccessStrategyTestCase extends Abs
Callable<Void> removeCallable = new Callable<Void>() {
public Void call() throws Exception {
removeLatch.await();
delegate.remove( "k1" );
CacheHelper.withinTx(localTm, new Callable<Void>() {
@Override
public Void call() throws Exception {
delegate.remove("k1");
return null;
}
});
pferLatch.countDown();
return null;
}
@ -355,26 +362,26 @@ public abstract class AbstractCollectionRegionAccessStrategyTestCase extends Abs
}
@Test
public void testRemove() {
public void testRemove() throws Exception {
evictOrRemoveTest( false );
}
@Test
public void testRemoveAll() {
public void testRemoveAll() throws Exception {
evictOrRemoveAllTest( false );
}
@Test
public void testEvict() {
public void testEvict() throws Exception {
evictOrRemoveTest( true );
}
@Test
public void testEvictAll() {
public void testEvictAll() throws Exception {
evictOrRemoveAllTest( true );
}
private void evictOrRemoveTest(boolean evict) {
private void evictOrRemoveTest(final boolean evict) throws Exception {
final String KEY = KEY_BASE + testCount++;
@ -389,19 +396,23 @@ public abstract class AbstractCollectionRegionAccessStrategyTestCase extends Abs
// Wait for async propagation
sleep( 250 );
if ( evict ) {
localAccessStrategy.evict( KEY );
}
else {
localAccessStrategy.remove( KEY );
CacheHelper.withinTx(localCollectionRegion.getTransactionManager(), new Callable<Void>() {
@Override
public Void call() throws Exception {
if (evict)
localAccessStrategy.evict(KEY);
else
localAccessStrategy.remove(KEY);
return null;
}
});
assertEquals( null, localAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertEquals( null, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
}
private void evictOrRemoveAllTest(boolean evict) {
private void evictOrRemoveAllTest(final boolean evict) throws Exception {
final String KEY = KEY_BASE + testCount++;
@ -420,12 +431,16 @@ public abstract class AbstractCollectionRegionAccessStrategyTestCase extends Abs
// Wait for async propagation
sleep( 250 );
if ( evict ) {
CacheHelper.withinTx(localCollectionRegion.getTransactionManager(), new Callable<Void>() {
@Override
public Void call() throws Exception {
if (evict)
localAccessStrategy.evictAll();
}
else {
else
localAccessStrategy.removeAll();
return null;
}
});
// This should re-establish the region root node
assertNull( localAccessStrategy.get( KEY, System.currentTimeMillis() ) );

View File

@ -76,7 +76,7 @@ public class CollectionRegionImplTestCase extends AbstractEntityCollectionRegion
@Override
protected CacheAdapter getInfinispanCache(InfinispanRegionFactory regionFactory) {
return CacheAdapterImpl.newInstance(regionFactory.getCacheManager().getCache(InfinispanRegionFactory.DEF_ENTITY_RESOURCE));
return CacheAdapterImpl.newInstance(regionFactory.getCacheManager().getCache(InfinispanRegionFactory.DEF_ENTITY_RESOURCE).getAdvancedCache());
}
@Override

View File

@ -23,9 +23,16 @@
*/
package org.hibernate.test.cache.infinispan.entity;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.hibernate.cache.infinispan.util.CacheHelper;
import org.infinispan.Cache;
import org.infinispan.test.TestingUtil;
import org.infinispan.transaction.tm.BatchModeTransactionManager;
import org.jboss.logging.Logger;
@ -105,6 +112,13 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
remoteEntityRegion = remoteEnvironment.getEntityRegion( REGION_NAME, getCacheDataDescription() );
remoteAccessStrategy = remoteEntityRegion.buildAccessStrategy( getAccessType() );
waitForClusterToForm(localEntityRegion.getCacheAdapter().getCache(),
remoteEntityRegion.getCacheAdapter().getCache());
}
protected void waitForClusterToForm(Cache... caches) {
TestingUtil.blockUntilViewsReceived(10000, Arrays.asList(caches));
}
protected abstract String getConfigurationName();
@ -503,26 +517,26 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
}
@Test
public void testRemove() {
public void testRemove() throws Exception {
evictOrRemoveTest( false );
}
@Test
public void testRemoveAll() {
public void testRemoveAll() throws Exception {
evictOrRemoveAllTest( false );
}
@Test
public void testEvict() {
public void testEvict() throws Exception {
evictOrRemoveTest( true );
}
@Test
public void testEvictAll() {
public void testEvictAll() throws Exception {
evictOrRemoveAllTest( true );
}
private void evictOrRemoveTest(boolean evict) {
private void evictOrRemoveTest(final boolean evict) throws Exception {
final String KEY = KEY_BASE + testCount++;
assertEquals( 0, getValidKeyCount( localEntityRegion.getCacheAdapter().keySet() ) );
assertEquals( 0, getValidKeyCount( remoteEntityRegion.getCacheAdapter().keySet() ) );
@ -535,20 +549,23 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
remoteAccessStrategy.putFromLoad( KEY, VALUE1, System.currentTimeMillis(), new Integer( 1 ) );
assertEquals( VALUE1, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
if ( evict ) {
CacheHelper.withinTx(localEntityRegion.getTransactionManager(), new Callable<Void>() {
@Override
public Void call() throws Exception {
if ( evict )
localAccessStrategy.evict( KEY );
}
else {
else
localAccessStrategy.remove( KEY );
return null;
}
assertEquals( null, localAccessStrategy.get( KEY, System.currentTimeMillis() ) );
});
assertEquals(null, localAccessStrategy.get(KEY, System.currentTimeMillis()));
assertEquals( 0, getValidKeyCount( localEntityRegion.getCacheAdapter().keySet() ) );
assertEquals( null, remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertEquals( 0, getValidKeyCount( remoteEntityRegion.getCacheAdapter().keySet() ) );
}
private void evictOrRemoveAllTest(boolean evict) {
private void evictOrRemoveAllTest(final boolean evict) throws Exception {
final String KEY = KEY_BASE + testCount++;
assertEquals( 0, getValidKeyCount( localEntityRegion.getCacheAdapter().keySet() ) );
assertEquals( 0, getValidKeyCount( remoteEntityRegion.getCacheAdapter().keySet() ) );
@ -567,16 +584,21 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
// Wait for async propagation
sleep( 250 );
if ( evict ) {
CacheHelper.withinTx(localEntityRegion.getTransactionManager(), new Callable<Void>() {
@Override
public Void call() throws Exception {
if (evict) {
log.debug("Call evict all locally");
localAccessStrategy.evictAll();
}
else {
} else {
localAccessStrategy.removeAll();
}
return null;
}
});
// This should re-establish the region root node in the optimistic case
assertNull( localAccessStrategy.get( KEY, System.currentTimeMillis() ) );
assertNull(localAccessStrategy.get(KEY, System.currentTimeMillis()));
assertEquals( 0, getValidKeyCount( localEntityRegion.getCacheAdapter().keySet() ) );
// Re-establishing the region root on the local node doesn't

View File

@ -87,7 +87,7 @@ public class EntityRegionImplTestCase extends AbstractEntityCollectionRegionTest
@Override
protected CacheAdapter getInfinispanCache(InfinispanRegionFactory regionFactory) {
return CacheAdapterImpl.newInstance(regionFactory.getCacheManager().getCache(InfinispanRegionFactory.DEF_ENTITY_RESOURCE));
return CacheAdapterImpl.newInstance(regionFactory.getCacheManager().getCache(InfinispanRegionFactory.DEF_ENTITY_RESOURCE).getAdvancedCache());
}
}

View File

@ -1,132 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.cache.infinispan.functional;
import javax.transaction.TransactionManager;
import java.util.Map;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory;
import org.hibernate.engine.transaction.spi.TransactionFactory;
import org.hibernate.stat.SecondLevelCacheStatistics;
import org.hibernate.stat.Statistics;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* BasicJdbcTransactionalTestCase.
*
* @author Galder Zamarreño
* @since 3.5
*/
public class BasicJdbcTransactionalTestCase extends SingleNodeTestCase {
private static final Log log = LogFactory.getLog( BasicJdbcTransactionalTestCase.class );
@Override
protected Class<? extends TransactionFactory> getTransactionFactoryClass() {
return JdbcTransactionFactory.class;
}
@Override
protected TransactionManager getTransactionManager() {
return null;
}
@Test
public void testCollectionCache() throws Exception {
Item item = new Item( "chris", "Chris's Item" );
Item another = new Item( "another", "Owned Item" );
item.addItem( another );
Session s = null;
try {
s = openSession();
s.beginTransaction();
s.persist( item );
s.persist( another );
s.getTransaction().commit();
}
catch (Exception e) {
log.error( "Exception", e );
s.getTransaction().rollback();
throw e;
}
finally {
s.close();
}
try {
s = openSession();
s.beginTransaction();
Item loaded = (Item) s.load( Item.class, item.getId() );
assertEquals( 1, loaded.getItems().size() );
}
catch (Exception e) {
log.error( "Exception", e );
s.getTransaction().rollback();
throw e;
}
finally {
s.close();
}
try {
s = openSession();
s.beginTransaction();
Statistics stats = s.getSessionFactory().getStatistics();
SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics( Item.class.getName() + ".items" );
Item loadedWithCachedCollection = (Item) s.load( Item.class, item.getId() );
stats.logSummary();
assertEquals( item.getName(), loadedWithCachedCollection.getName() );
assertEquals( item.getItems().size(), loadedWithCachedCollection.getItems().size() );
assertEquals( 1, cStats.getHitCount() );
Map cacheEntries = cStats.getEntries();
assertEquals( 1, cacheEntries.size() );
}
catch (Exception e) {
log.error( "Exception", e );
s.getTransaction().rollback();
throw e;
}
finally {
s.close();
}
}
@Test
public void testEmptySecondLevelCacheEntry() throws Exception {
sessionFactory().getCache().evictCollectionRegion( Item.class.getName() + ".items" );
Statistics stats = sessionFactory().getStatistics();
stats.clear();
SecondLevelCacheStatistics statistics = stats.getSecondLevelCacheStatistics( Item.class.getName() + ".items" );
Map cacheEntries = statistics.getEntries();
assertEquals( 0, cacheEntries.size() );
}
}

View File

@ -35,8 +35,6 @@ import org.infinispan.notifications.cachelistener.event.CacheEntryModifiedEvent;
import org.infinispan.notifications.cachelistener.event.CacheEntryVisitedEvent;
import org.jboss.logging.Logger;
import org.hibernate.cache.infinispan.util.CacheHelper;
@Listener
public class CacheAccessListener {
private static final Logger log = Logger.getLogger( CacheAccessListener.class );
@ -51,7 +49,7 @@ public class CacheAccessListener {
@CacheEntryModified
public void nodeModified( CacheEntryModifiedEvent event ) {
if (!event.isPre() && !CacheHelper.isEvictAllNotification(event.getKey())) {
if (!event.isPre()) {
Object key = event.getKey();
log.info("Modified node " + key);
modified.add(key.toString());
@ -60,7 +58,7 @@ public class CacheAccessListener {
@CacheEntryCreated
public void nodeCreated( CacheEntryCreatedEvent event ) {
if (!event.isPre() && !CacheHelper.isEvictAllNotification(event.getKey())) {
if (!event.isPre()) {
Object key = event.getKey();
log.info("Created node " + key);
modified.add(key.toString());
@ -69,7 +67,7 @@ public class CacheAccessListener {
@CacheEntryVisited
public void nodeVisited( CacheEntryVisitedEvent event ) {
if (!event.isPre() && !CacheHelper.isEvictAllNotification(event.getKey())) {
if (!event.isPre()) {
Object key = event.getKey();
log.info("Visited node " + key);
accessed.add(key.toString());

View File

@ -40,7 +40,6 @@ import org.jboss.util.collection.ConcurrentSet;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cache.spi.CacheKey;
import org.hibernate.cache.infinispan.util.CacheHelper;
import org.junit.Test;
@ -340,13 +339,7 @@ public class EntityCollectionInvalidationTestCase extends DualNodeTestCase {
}
protected int getValidKeyCount(Set keys) {
int result = 0;
for ( Object key : keys ) {
if ( !(CacheHelper.isEvictAllNotification( key )) ) {
result++;
}
}
return result;
return keys.size();
}
@Listener

View File

@ -24,9 +24,12 @@
package org.hibernate.test.cache.infinispan.query;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.hibernate.cache.infinispan.util.CacheHelper;
import org.hibernate.cache.spi.GeneralDataRegion;
import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryVisited;
import org.infinispan.notifications.cachelistener.event.CacheEntryVisitedEvent;
@ -76,9 +79,31 @@ public class QueryRegionImplTestCase extends AbstractGeneralDataRegionTestCase {
return regionPrefix + "/" + StandardQueryCache.class.getName();
}
@Override
protected void regionPut(final GeneralDataRegion region) throws Exception {
CacheHelper.withinTx(BatchModeTransactionManager.getInstance(), new Callable<Void>() {
@Override
public Void call() throws Exception {
region.put(KEY, VALUE1);
return null;
}
});
}
@Override
protected void regionEvict(final GeneralDataRegion region) throws Exception {
CacheHelper.withinTx(BatchModeTransactionManager.getInstance(), new Callable<Void>() {
@Override
public Void call() throws Exception {
region.evict(KEY);
return null;
}
});
}
@Override
protected CacheAdapter getInfinispanCache(InfinispanRegionFactory regionFactory) {
return CacheAdapterImpl.newInstance( regionFactory.getCacheManager().getCache( "local-query" ) );
return CacheAdapterImpl.newInstance(regionFactory.getCacheManager().getCache( "local-query" ).getAdvancedCache());
}
@Override

View File

@ -76,7 +76,7 @@ public class TimestampsRegionImplTestCase extends AbstractGeneralDataRegionTestC
@Override
protected CacheAdapter getInfinispanCache(InfinispanRegionFactory regionFactory) {
return CacheAdapterImpl.newInstance(regionFactory.getCacheManager().getCache("timestamps"));
return CacheAdapterImpl.newInstance(regionFactory.getCacheManager().getCache("timestamps").getAdvancedCache());
}
public void testClearTimestampsRegionInIsolated() throws Exception {
@ -141,7 +141,7 @@ public class TimestampsRegionImplTestCase extends AbstractGeneralDataRegionTestC
// }
@Override
protected ClassLoaderAwareCache createCacheWrapper(AdvancedCache cache) {
protected AdvancedCache createCacheWrapper(AdvancedCache cache) {
return new ClassLoaderAwareCache(cache, Thread.currentThread().getContextClassLoader()) {
@Override
public void addListener(Object listener) {

View File

@ -26,6 +26,7 @@ import java.util.Enumeration;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@ -35,6 +36,7 @@ import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.Settings;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.classloading.internal.ClassLoaderServiceImpl;
/**
* Utilities for cache testing.
@ -80,9 +82,7 @@ public class CacheTestUtil {
String factoryType = cfg.getProperty(Environment.CACHE_REGION_FACTORY);
Class factoryClass = Thread.currentThread().getContextClassLoader().loadClass(factoryType);
InfinispanRegionFactory regionFactory = (InfinispanRegionFactory) factoryClass.newInstance();
regionFactory.start(settings, properties);
return regionFactory;
}