From fa8e94071fbfc8ff0f9ab7a493bcf3bb612b7a50 Mon Sep 17 00:00:00 2001 From: Radim Vansa Date: Tue, 28 Jul 2015 16:59:54 +0200 Subject: [PATCH] HHH-9988 Separate transaction manager used for Hibernate and caches --- .../cache/infinispan/InfinispanRegionFactory.java | 14 +++++++++----- .../infinispan/access/PutFromLoadValidator.java | 10 ++++++---- .../collection/CollectionRegionImpl.java | 9 ++++++--- .../cache/infinispan/entity/EntityRegionImpl.java | 9 ++++++--- .../infinispan/impl/BaseGeneralDataRegion.java | 2 +- .../cache/infinispan/impl/BaseRegion.java | 5 +++-- .../impl/BaseTransactionalDataRegion.java | 7 +++++-- .../infinispan/naturalid/NaturalIdRegionImpl.java | 9 ++++++--- .../infinispan/query/QueryResultsRegionImpl.java | 4 ++-- 9 files changed, 44 insertions(+), 25 deletions(-) diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java index 32042ed4e2..509373ce00 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java @@ -67,6 +67,8 @@ import org.infinispan.util.concurrent.IsolationLevel; import org.infinispan.util.logging.Log; import org.infinispan.util.logging.LogFactory; +import javax.transaction.TransactionManager; + /** * A {@link RegionFactory} for Infinispan-backed cache * regions. @@ -219,6 +221,7 @@ public class InfinispanRegionFactory implements RegionFactory { private final Set definedConfigurations = new HashSet(); private org.infinispan.transaction.lookup.TransactionManagerLookup transactionManagerlookup; + private TransactionManager transactionManager; private List regionNames = new ArrayList(); private SessionFactoryOptions settings; @@ -247,7 +250,7 @@ public class InfinispanRegionFactory implements RegionFactory { log.debug( "Building collection cache region [" + regionName + "]" ); } final AdvancedCache cache = getCache( regionName, COLLECTION_KEY, properties, metadata); - final CollectionRegionImpl region = new CollectionRegionImpl( cache, regionName, metadata, this, buildCacheKeysFactory() ); + final CollectionRegionImpl region = new CollectionRegionImpl( cache, regionName, transactionManager, metadata, this, buildCacheKeysFactory() ); startRegion( region, regionName ); return region; } @@ -264,7 +267,7 @@ public class InfinispanRegionFactory implements RegionFactory { ); } final AdvancedCache cache = getCache( regionName, metadata.isMutable() ? ENTITY_KEY : IMMUTABLE_ENTITY_KEY, properties, metadata ); - final EntityRegionImpl region = new EntityRegionImpl( cache, regionName, metadata, this, buildCacheKeysFactory() ); + final EntityRegionImpl region = new EntityRegionImpl( cache, regionName, transactionManager, metadata, this, buildCacheKeysFactory() ); startRegion( region, regionName ); return region; } @@ -276,7 +279,7 @@ public class InfinispanRegionFactory implements RegionFactory { log.debug("Building natural id cache region [" + regionName + "]"); } final AdvancedCache cache = getCache( regionName, NATURAL_ID_KEY, properties, metadata); - final NaturalIdRegionImpl region = new NaturalIdRegionImpl( cache, regionName, metadata, this, buildCacheKeysFactory()); + final NaturalIdRegionImpl region = new NaturalIdRegionImpl( cache, regionName, transactionManager, metadata, this, buildCacheKeysFactory()); startRegion( region, regionName ); return region; } @@ -294,7 +297,7 @@ public class InfinispanRegionFactory implements RegionFactory { } final AdvancedCache cache = getCache( cacheName, QUERY_KEY, properties, null); - final QueryResultsRegionImpl region = new QueryResultsRegionImpl( cache, regionName, this ); + final QueryResultsRegionImpl region = new QueryResultsRegionImpl( cache, regionName, transactionManager, this ); startRegion( region, regionName ); return region; } @@ -358,6 +361,7 @@ public class InfinispanRegionFactory implements RegionFactory { log.debug( "Starting Infinispan region factory" ); try { transactionManagerlookup = createTransactionManagerLookup( settings, properties ); + transactionManager = transactionManagerlookup.getTransactionManager(); manager = createCacheManager( properties, settings.getServiceRegistry() ); this.settings = settings; initGenericDataTypeOverrides(); @@ -602,7 +606,7 @@ public class InfinispanRegionFactory implements RegionFactory { } } - private AdvancedCache getCache(String regionName, String typeKey, Properties properties, CacheDataDescription metadata) { + protected AdvancedCache getCache(String regionName, String typeKey, Properties properties, CacheDataDescription metadata) { TypeOverrides regionOverride = typeOverrides.get( regionName ); if ( !definedConfigurations.contains( regionName ) ) { final String templateCacheName; diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/access/PutFromLoadValidator.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/access/PutFromLoadValidator.java index 69edd85bbf..0229a85b1d 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/access/PutFromLoadValidator.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/access/PutFromLoadValidator.java @@ -114,24 +114,26 @@ public class PutFromLoadValidator { * Creates a new put from load validator instance. * * @param cache Cache instance on which to store pending put information. + * @param transactionManager Transaction manager */ - public PutFromLoadValidator(AdvancedCache cache) { - this( cache, NAKED_PUT_INVALIDATION_PERIOD ); + public PutFromLoadValidator(AdvancedCache cache, TransactionManager transactionManager) { + this( cache, transactionManager, NAKED_PUT_INVALIDATION_PERIOD ); } /** * Constructor variant for use by unit tests; allows control of various timeouts by the test. * * @param cache Cache instance on which to store pending put information. + * @param transactionManager Transaction manager * @param nakedPutInvalidationPeriod Period (in ms) after a removal during which a call to * {@link #acquirePutFromLoadLock(Object)} that hasn't been * {@link #registerPendingPut(Object) pre-registered} (aka a "naked put") * will return false. */ public PutFromLoadValidator( - AdvancedCache cache, + AdvancedCache cache, TransactionManager transactionManager, long nakedPutInvalidationPeriod) { - this(cache, cache.getCacheManager(), cache.getTransactionManager(), + this(cache, cache.getCacheManager(), transactionManager, nakedPutInvalidationPeriod ); } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/collection/CollectionRegionImpl.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/collection/CollectionRegionImpl.java index 869c6bef24..bd016979e4 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/collection/CollectionRegionImpl.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/collection/CollectionRegionImpl.java @@ -17,6 +17,8 @@ import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.infinispan.AdvancedCache; +import javax.transaction.TransactionManager; + /** * Collection region implementation * @@ -31,14 +33,15 @@ public class CollectionRegionImpl extends BaseTransactionalDataRegion implements * * @param cache instance to store collection instances * @param name of collection type + * @param transactionManager * @param metadata for the collection type * @param factory for the region * @param cacheKeysFactory factory for cache keys */ public CollectionRegionImpl( - AdvancedCache cache, String name, + AdvancedCache cache, String name, TransactionManager transactionManager, CacheDataDescription metadata, RegionFactory factory, CacheKeysFactory cacheKeysFactory) { - super( cache, name, metadata, factory, cacheKeysFactory ); + super( cache, name, transactionManager, metadata, factory, cacheKeysFactory ); } @Override @@ -52,7 +55,7 @@ public class CollectionRegionImpl extends BaseTransactionalDataRegion implements } public PutFromLoadValidator getPutFromLoadValidator() { - return new PutFromLoadValidator( cache ); + return new PutFromLoadValidator( cache, getTransactionManager() ); } } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/entity/EntityRegionImpl.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/entity/EntityRegionImpl.java index 1cc5215a54..ba4eb26222 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/entity/EntityRegionImpl.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/entity/EntityRegionImpl.java @@ -18,6 +18,8 @@ import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.infinispan.AdvancedCache; +import javax.transaction.TransactionManager; + /** * Entity region implementation * @@ -32,14 +34,15 @@ public class EntityRegionImpl extends BaseTransactionalDataRegion implements Ent * * @param cache instance to store entity instances * @param name of entity type + * @param transactionManager * @param metadata for the entity type * @param factory for the region * @param cacheKeysFactory factory for cache keys */ public EntityRegionImpl( - AdvancedCache cache, String name, + AdvancedCache cache, String name, TransactionManager transactionManager, CacheDataDescription metadata, RegionFactory factory, CacheKeysFactory cacheKeysFactory) { - super( cache, name, metadata, factory, cacheKeysFactory); + super( cache, name, transactionManager, metadata, factory, cacheKeysFactory); } @Override @@ -60,6 +63,6 @@ public class EntityRegionImpl extends BaseTransactionalDataRegion implements Ent } public PutFromLoadValidator getPutFromLoadValidator() { - return new PutFromLoadValidator( cache ); + return new PutFromLoadValidator( cache, getTransactionManager() ); } } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseGeneralDataRegion.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseGeneralDataRegion.java index 161fa804aa..97a687bcf2 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseGeneralDataRegion.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseGeneralDataRegion.java @@ -34,7 +34,7 @@ public abstract class BaseGeneralDataRegion extends BaseRegion implements Genera public BaseGeneralDataRegion( AdvancedCache cache, String name, RegionFactory factory) { - super( cache, name, factory ); + super( cache, name, null, factory ); this.putCache = Caches.ignoreReturnValuesCache( cache ); } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseRegion.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseRegion.java index 0fbb5dfb0c..f6cc027741 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseRegion.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseRegion.java @@ -59,12 +59,13 @@ public abstract class BaseRegion implements Region { * * @param cache instance for the region * @param name of the region + * @param transactionManager transaction manager may be needed even for non-transactional caches. * @param factory for this region */ - public BaseRegion(AdvancedCache cache, String name, RegionFactory factory) { + public BaseRegion(AdvancedCache cache, String name, TransactionManager transactionManager, RegionFactory factory) { this.cache = cache; this.name = name; - this.tm = cache.getTransactionManager(); + this.tm = transactionManager; this.factory = factory; this.localAndSkipLoadCache = cache.withFlags( Flag.CACHE_MODE_LOCAL, Flag.ZERO_LOCK_ACQUISITION_TIMEOUT, diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseTransactionalDataRegion.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseTransactionalDataRegion.java index 06a81dba02..8011334d50 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseTransactionalDataRegion.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseTransactionalDataRegion.java @@ -13,6 +13,8 @@ import org.hibernate.cache.spi.TransactionalDataRegion; import org.infinispan.AdvancedCache; +import javax.transaction.TransactionManager; + /** * Support for Inifinispan {@link org.hibernate.cache.spi.TransactionalDataRegion} implementors. * @@ -31,14 +33,15 @@ public abstract class BaseTransactionalDataRegion * * @param cache instance to store transactional data * @param name of the transactional region + * @param transactionManager * @param metadata for the transactional region * @param factory for the transactional region * @param cacheKeysFactory factory for cache keys */ public BaseTransactionalDataRegion( - AdvancedCache cache, String name, + AdvancedCache cache, String name, TransactionManager transactionManager, CacheDataDescription metadata, RegionFactory factory, CacheKeysFactory cacheKeysFactory) { - super( cache, name, factory); + super( cache, name, transactionManager, factory); this.metadata = metadata; this.cacheKeysFactory = cacheKeysFactory; } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/NaturalIdRegionImpl.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/NaturalIdRegionImpl.java index 3c7b6d82f4..8aed83fd9e 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/NaturalIdRegionImpl.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/NaturalIdRegionImpl.java @@ -17,6 +17,8 @@ import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; import org.infinispan.AdvancedCache; +import javax.transaction.TransactionManager; + /** * Natural ID cache region * @@ -31,14 +33,15 @@ public class NaturalIdRegionImpl extends BaseTransactionalDataRegion * * @param cache instance to store natural ids * @param name of natural id region + * @param transactionManager * @param metadata for the natural id region * @param factory for the natural id region * @param cacheKeysFactory factory for cache keys */ public NaturalIdRegionImpl( - AdvancedCache cache, String name, + AdvancedCache cache, String name, TransactionManager transactionManager, CacheDataDescription metadata, RegionFactory factory, CacheKeysFactory cacheKeysFactory) { - super( cache, name, metadata, factory, cacheKeysFactory ); + super( cache, name, transactionManager, metadata, factory, cacheKeysFactory ); } @Override @@ -54,7 +57,7 @@ public class NaturalIdRegionImpl extends BaseTransactionalDataRegion } public PutFromLoadValidator getPutFromLoadValidator() { - return new PutFromLoadValidator( cache ); + return new PutFromLoadValidator( cache, getTransactionManager() ); } } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/query/QueryResultsRegionImpl.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/query/QueryResultsRegionImpl.java index 9e1ec82053..9274e46bc7 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/query/QueryResultsRegionImpl.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/query/QueryResultsRegionImpl.java @@ -58,8 +58,8 @@ public class QueryResultsRegionImpl extends BaseTransactionalDataRegion implemen * @param name of the query region * @param factory for the query region */ - public QueryResultsRegionImpl(AdvancedCache cache, String name, RegionFactory factory) { - super( cache, name, null, factory, null ); + public QueryResultsRegionImpl(AdvancedCache cache, String name, TransactionManager transactionManager, RegionFactory factory) { + super( cache, name, transactionManager, null, factory, null ); // If Infinispan is using INVALIDATION for query cache, we don't want to propagate changes. // We use the Timestamps cache to manage invalidation final boolean localOnly = Caches.isInvalidationCache( cache );