HHH-10287 - Cache keys no longer include the entity type

This commit is contained in:
Gail Badner 2016-06-07 15:30:27 -07:00
parent d2c91c36d0
commit 1ed76e801b
20 changed files with 359 additions and 82 deletions

View File

@ -0,0 +1,14 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.boot.registry.selector.spi;
/**
* @author Steve Ebersole
*/
public interface StrategyCreator<T> {
T create(Class<? extends T> strategyClass);
}

View File

@ -19,6 +19,7 @@ import org.hibernate.SessionFactoryObserver;
import org.hibernate.boot.SchemaAutoTooling;
import org.hibernate.boot.TempTableDdlTransactionHandling;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.cache.spi.CacheKeysFactory;
import org.hibernate.cache.spi.QueryCacheFactory;
import org.hibernate.cfg.BaselineSessionEventsListenerBuilder;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;

View File

@ -9,17 +9,16 @@ package org.hibernate.cache.internal;
import java.util.Map;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.boot.registry.selector.spi.StrategySelectionException;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.jboss.logging.Logger;
/**
* Initiator for the {@link RegionFactory} service.
*
@ -27,9 +26,7 @@ import org.jboss.logging.Logger;
* @author Brett Meyer
*/
public class RegionFactoryInitiator implements StandardServiceInitiator<RegionFactory> {
private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class,
RegionFactoryInitiator.class.getName() );
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( RegionFactoryInitiator.class );
/**
* Singleton access
@ -63,26 +60,15 @@ public class RegionFactoryInitiator implements StandardServiceInitiator<RegionFa
// The cache provider is needed when we either have second-level cache enabled
// or query cache enabled. Note that useSecondLevelCache is enabled by default
final String setting = ConfigurationHelper.getString( AvailableSettings.CACHE_REGION_FACTORY,
configurationValues, null );
if ( ( useSecondLevelCache || useQueryCache ) && setting != null ) {
try {
final Class<? extends RegionFactory> regionFactoryClass = registry.getService( StrategySelector.class )
.selectStrategyImplementor( RegionFactory.class, setting );
try {
regionFactory = regionFactoryClass.getConstructor( Properties.class ).newInstance( p );
}
catch ( NoSuchMethodException e ) {
// no constructor accepting Properties found, try no arg constructor
LOG.debugf(
"%s did not provide constructor accepting java.util.Properties; attempting no-arg constructor.",
regionFactoryClass.getSimpleName() );
regionFactory = regionFactoryClass.getConstructor().newInstance();
}
}
catch ( Exception e ) {
throw new HibernateException( "could not instantiate RegionFactory [" + setting + "]", e );
}
if ( useSecondLevelCache || useQueryCache ) {
final Object strategyReference = configurationValues != null
? configurationValues.get( AvailableSettings.CACHE_REGION_FACTORY )
: null;
regionFactory = resolveRegionFactory(
registry.getService( StrategySelector.class ),
strategyReference,
p
);
}
LOG.debugf( "Cache region factory : %s", regionFactory.getClass().getName() );
@ -90,6 +76,43 @@ public class RegionFactoryInitiator implements StandardServiceInitiator<RegionFa
return regionFactory;
}
@SuppressWarnings("unchecked")
private RegionFactory resolveRegionFactory(
StrategySelector strategySelector,
Object strategyReference,
Properties properties) {
if ( strategyReference == null || RegionFactory.class.isInstance( strategyReference ) ) {
// nothing to create; just let strategySelector resolve the RegionFactory
return strategySelector.resolveDefaultableStrategy(
RegionFactory.class,
strategyReference,
NoCachingRegionFactory.INSTANCE
);
}
final Class<? extends RegionFactory> implementationClass;
if ( Class.class.isInstance( strategyReference ) ) {
implementationClass = (Class<RegionFactory>) strategyReference;
}
else {
implementationClass = strategySelector.selectStrategyImplementor(
RegionFactory.class,
strategyReference.toString()
);
}
try {
return new StrategyCreatorRegionFactoryImpl( properties ).create( implementationClass );
}
catch (Exception e) {
throw new StrategySelectionException(
String.format( "Could not instantiate named strategy class [%s]", implementationClass.getName() ),
e
);
}
}
/**
* Map legacy names unto the new corollary.
*

View File

@ -0,0 +1,83 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.cache.internal;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Properties;
import org.hibernate.boot.registry.selector.spi.StrategyCreator;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.service.spi.ServiceException;
import org.jboss.logging.Logger;
/**
* @author Steve Ebersole
*/
public class StrategyCreatorRegionFactoryImpl implements StrategyCreator<RegionFactory> {
private static final Logger log = Logger.getLogger( StrategyCreatorRegionFactoryImpl.class );
private final Properties properties;
public StrategyCreatorRegionFactoryImpl(Properties properties) {
this.properties = properties;
}
@Override
public RegionFactory create(Class<? extends RegionFactory> strategyClass) {
assert RegionFactory.class.isAssignableFrom( strategyClass );
// first look for a constructor accepting Properties
try {
final Constructor<? extends RegionFactory> ctor = strategyClass.getConstructor( Properties.class );
return ctor.newInstance( properties );
}
catch ( NoSuchMethodException e ) {
log.debugf( "RegionFactory impl [%s] did not provide constructor accepting Properties", strategyClass.getName() );
}
catch ( IllegalAccessException e ) {
throw new ServiceException( "Unable to call constructor of RegionFactory impl [" + strategyClass.getName() + "]", e );
}
catch ( InstantiationException e ) {
throw new ServiceException( "Unable to call constructor of RegionFactory impl [" + strategyClass.getName() + "]", e );
}
catch ( InvocationTargetException e ) {
throw new ServiceException( "Unable to call constructor of RegionFactory impl [" + strategyClass.getName() + "]", e );
}
// next try Map
try {
final Constructor<? extends RegionFactory> ctor = strategyClass.getConstructor( Map.class );
return ctor.newInstance( properties );
}
catch ( NoSuchMethodException e ) {
log.debugf( "RegionFactory impl [%s] did not provide constructor accepting Properties", strategyClass.getName() );
}
catch ( IllegalAccessException e ) {
throw new ServiceException( "Unable to call constructor of RegionFactory impl [" + strategyClass.getName() + "]", e );
}
catch ( InstantiationException e ) {
throw new ServiceException( "Unable to call constructor of RegionFactory impl [" + strategyClass.getName() + "]", e );
}
catch ( InvocationTargetException e ) {
throw new ServiceException( "Unable to call constructor of RegionFactory impl [" + strategyClass.getName() + "]", e );
}
// finally try no-arg
try {
return strategyClass.newInstance();
}
catch ( IllegalAccessException e ) {
throw new ServiceException( "Unable to call constructor of RegionFactory impl [" + strategyClass.getName() + "]", e );
}
catch ( InstantiationException e ) {
throw new ServiceException( "Unable to call constructor of RegionFactory impl [" + strategyClass.getName() + "]", e );
}
}
}

View File

@ -668,14 +668,31 @@ public interface AvailableSettings {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* The {@link org.hibernate.cache.spi.RegionFactory} implementation class
* The {@link org.hibernate.cache.spi.RegionFactory} implementation. Can refer to:<ul>
* <li>an Object implementing {@link org.hibernate.cache.spi.RegionFactory}</li>
* <li>a Class implementing {@link org.hibernate.cache.spi.RegionFactory}</li>
* <li>FQN of a Class implementing {@link org.hibernate.cache.spi.RegionFactory}</li>
* </ul>
*/
String CACHE_REGION_FACTORY = "hibernate.cache.region.factory_class";
/**
* Allow control to specify the {@link org.hibernate.cache.spi.CacheKeysFactory} impl to use.
* Can refer to:<ul>
* <li>an Object implementing {@link org.hibernate.cache.spi.CacheKeysFactory}</li>
* <li>a Class implementing {@link org.hibernate.cache.spi.CacheKeysFactory}</li>
* <li>FQN of a Class implementing {@link org.hibernate.cache.spi.CacheKeysFactory}</li>
* </ul>
*
* @since 5.2 - note that currently this is only honored for hibernate-infinispan
*/
String CACHE_KEYS_FACTORY = "hibernate.cache.keys_factory";
/**
* The <tt>CacheProvider</tt> implementation class
*/
String CACHE_PROVIDER_CONFIG = "hibernate.cache.provider_configuration_file_resource_path";
/**
* Enable the second-level cache (enabled by default)
*/

View File

@ -0,0 +1,94 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.cache;
import java.util.Properties;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.Session;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.internal.DefaultCacheKeysFactory;
import org.hibernate.cache.internal.SimpleCacheKeysFactory;
import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cache.spi.EntityRegion;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.cache.CachingRegionFactory;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertNull;
/**
* @author Steve Ebersole
*/
public class SharedRegionTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) {
super.configureStandardServiceRegistryBuilder( ssrb );
ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, true );
ssrb.applySetting( AvailableSettings.CACHE_REGION_FACTORY, new CachingRegionFactory() );
}
@Override
protected void applyMetadataSources(MetadataSources metadataSources) {
super.applyMetadataSources( metadataSources );
metadataSources.addAnnotatedClass( StateCodes.class );
metadataSources.addAnnotatedClass( ZipCodes.class );
}
@Test
public void test() {
// create a StateCodes
Session s = openSession();
s.beginTransaction();
s.save( new StateCodes( 1 ) );
s.getTransaction().commit();
s.close();
// now try to load a ZipCodes using the same id : should just return null rather than blow up :)
s = openSession();
s.beginTransaction();
ZipCodes zc = s.get( ZipCodes.class, 1 );
assertNull( zc );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.get( ZipCodes.class, 1 );
s.getTransaction().commit();
s.close();
}
@Entity( name="StateCodes" )
@Cache( region="com.acme.referenceData", usage = CacheConcurrencyStrategy.READ_WRITE )
public static class StateCodes {
@Id
public Integer id;
public StateCodes() {
}
public StateCodes(Integer id) {
this.id = id;
}
}
@Entity( name = "ZipCodes" )
@Cache( region="com.acme.referenceData", usage = CacheConcurrencyStrategy.READ_WRITE )
public static class ZipCodes {
@Id
public Integer id;
}
}

View File

@ -21,6 +21,7 @@ import java.util.concurrent.TimeUnit;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.MultiTenancyStrategy;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.infinispan.collection.CollectionRegionImpl;
@ -45,6 +46,7 @@ import org.hibernate.cache.spi.QueryResultsRegion;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.TimestampsRegion;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.ServiceRegistry;
@ -229,6 +231,7 @@ public class InfinispanRegionFactory implements RegionFactory {
private Configuration pendingPutsCacheConfiguration;
private CacheKeysFactory cacheKeysFactory;
private EmbeddedCacheManager manager;
private final Map<String, TypeOverrides> typeOverrides = new HashMap<String, TypeOverrides>();
@ -257,6 +260,7 @@ public class InfinispanRegionFactory implements RegionFactory {
}
@Override
@SuppressWarnings("unchecked")
public CollectionRegion buildCollectionRegion(
String regionName,
Properties properties,
@ -265,7 +269,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, transactionManager, metadata, this, buildCacheKeysFactory() );
final CollectionRegionImpl region = new CollectionRegionImpl( cache, regionName, transactionManager, metadata, this, getCacheKeysFactory() );
startRegion( region );
return region;
}
@ -282,7 +286,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, transactionManager, metadata, this, buildCacheKeysFactory() );
final EntityRegionImpl region = new EntityRegionImpl( cache, regionName, transactionManager, metadata, this, getCacheKeysFactory() );
startRegion( region );
return region;
}
@ -294,7 +298,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, transactionManager, metadata, this, buildCacheKeysFactory());
final NaturalIdRegionImpl region = new NaturalIdRegionImpl( cache, regionName, transactionManager, metadata, this, getCacheKeysFactory());
startRegion( region );
return region;
}
@ -318,8 +322,8 @@ public class InfinispanRegionFactory implements RegionFactory {
}
@Override
public TimestampsRegion buildTimestampsRegion(String regionName, Properties properties)
throws CacheException {
@SuppressWarnings("unchecked")
public TimestampsRegion buildTimestampsRegion(String regionName, Properties properties) {
if ( log.isDebugEnabled() ) {
log.debug( "Building timestamps cache region [" + regionName + "]" );
}
@ -339,13 +343,8 @@ public class InfinispanRegionFactory implements RegionFactory {
}
}
private CacheKeysFactory buildCacheKeysFactory() {
if (settings.getMultiTenancyStrategy() != MultiTenancyStrategy.NONE) {
return DefaultCacheKeysFactory.INSTANCE;
}
else {
return SimpleCacheKeysFactory.INSTANCE;
}
private CacheKeysFactory getCacheKeysFactory() {
return cacheKeysFactory;
}
@Override
@ -374,6 +373,10 @@ public class InfinispanRegionFactory implements RegionFactory {
@Override
public void start(SessionFactoryOptions settings, Properties properties) throws CacheException {
log.debug( "Starting Infinispan region factory" );
// determine the CacheKeysFactory to use...
this.cacheKeysFactory = determineCacheKeysFactory( settings, properties );
try {
transactionManagerlookup = createTransactionManagerLookup( settings, properties );
transactionManager = transactionManagerlookup.getTransactionManager();
@ -416,6 +419,18 @@ public class InfinispanRegionFactory implements RegionFactory {
}
}
private CacheKeysFactory determineCacheKeysFactory(SessionFactoryOptions settings, Properties properties) {
final CacheKeysFactory implicitFactory = settings.getMultiTenancyStrategy() != MultiTenancyStrategy.NONE
? DefaultCacheKeysFactory.INSTANCE
: SimpleCacheKeysFactory.INSTANCE;
return settings.getServiceRegistry().getService( StrategySelector.class ).resolveDefaultableStrategy(
CacheKeysFactory.class,
properties.get( AvailableSettings.CACHE_KEYS_FACTORY ),
implicitFactory
);
}
protected org.infinispan.transaction.lookup.TransactionManagerLookup createTransactionManagerLookup(
SessionFactoryOptions settings, Properties properties) {
return new HibernateTransactionManagerLookup( settings, properties );

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.testing.cache;
import org.hibernate.cache.internal.DefaultCacheKeysFactory;
import org.hibernate.cache.spi.CollectionRegion;
import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -40,12 +39,12 @@ class BaseCollectionRegionAccessStrategy extends BaseRegionAccessStrategy implem
@Override
public Object generateCacheKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
return DefaultCacheKeysFactory.createCollectionKey( id, persister, factory, tenantIdentifier );
return region.getRegionFactory().getCacheKeysFactory().createCollectionKey( id, persister, factory, tenantIdentifier );
}
@Override
public Object getCacheKeyId(Object cacheKey) {
return DefaultCacheKeysFactory.getCollectionId(cacheKey);
return region.getRegionFactory().getCacheKeysFactory().getCollectionId( cacheKey );
}
}

View File

@ -7,7 +7,6 @@
package org.hibernate.testing.cache;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.internal.DefaultCacheKeysFactory;
import org.hibernate.cache.spi.EntityRegion;
import org.hibernate.cache.spi.access.EntityRegionAccessStrategy;
import org.hibernate.cache.spi.access.SoftLock;
@ -65,11 +64,11 @@ class BaseEntityRegionAccessStrategy extends BaseRegionAccessStrategy implements
@Override
public Object generateCacheKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
return DefaultCacheKeysFactory.createEntityKey( id, persister, factory, tenantIdentifier );
return region.getRegionFactory().getCacheKeysFactory().createEntityKey( id, persister, factory, tenantIdentifier );
}
@Override
public Object getCacheKeyId(Object cacheKey) {
return DefaultCacheKeysFactory.getEntityId(cacheKey);
return region.getRegionFactory().getCacheKeysFactory().getEntityId( cacheKey );
}
}

View File

@ -18,8 +18,8 @@ import org.jboss.logging.Logger;
class BaseGeneralDataRegion extends BaseRegion implements GeneralDataRegion {
private static final Logger LOG = Logger.getLogger( BaseGeneralDataRegion.class.getName() );
BaseGeneralDataRegion(String name) {
super( name );
BaseGeneralDataRegion(CachingRegionFactory cachingRegionFactory, String name) {
super( cachingRegionFactory, name );
}
@Override

View File

@ -7,7 +7,6 @@
package org.hibernate.testing.cache;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.internal.DefaultCacheKeysFactory;
import org.hibernate.cache.spi.NaturalIdRegion;
import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy;
import org.hibernate.cache.spi.access.SoftLock;
@ -61,11 +60,11 @@ class BaseNaturalIdRegionAccessStrategy extends BaseRegionAccessStrategy impleme
@Override
public Object generateCacheKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) {
return DefaultCacheKeysFactory.createNaturalIdKey( naturalIdValues, persister, session );
return region.getRegionFactory().getCacheKeysFactory().createNaturalIdKey( naturalIdValues, persister, session );
}
@Override
public Object[] getNaturalIdValues(Object cacheKey) {
return DefaultCacheKeysFactory.getNaturalIdValues( cacheKey );
return region.getRegionFactory().getCacheKeysFactory().getNaturalIdValues( cacheKey );
}
}

View File

@ -17,11 +17,14 @@ import org.hibernate.cache.spi.Region;
* @author Strong Liu
*/
class BaseRegion implements Region {
protected final Map cache = new ConcurrentHashMap();
private final CachingRegionFactory cachingRegionFactory;
private final String name;
protected final Map cache = new ConcurrentHashMap();
private static int timeout = Timestamper.ONE_MS * 60000; //60s
BaseRegion(String name) {
BaseRegion(CachingRegionFactory cachingRegionFactory, String name) {
this.cachingRegionFactory = cachingRegionFactory;
this.name = name;
}
@ -30,6 +33,10 @@ class BaseRegion implements Region {
return key != null ? cache.containsKey( key ) : false;
}
public CachingRegionFactory getRegionFactory() {
return cachingRegionFactory;
}
@Override
public String getName() {
return name;

View File

@ -15,8 +15,8 @@ import org.hibernate.cache.spi.TransactionalDataRegion;
class BaseTransactionalDataRegion extends BaseGeneralDataRegion implements TransactionalDataRegion {
private final CacheDataDescription metadata;
BaseTransactionalDataRegion(String name, CacheDataDescription metadata) {
super( name );
BaseTransactionalDataRegion(CachingRegionFactory cachingRegionFactory, String name, CacheDataDescription metadata) {
super( cachingRegionFactory, name );
this.metadata = metadata;
}

View File

@ -10,7 +10,9 @@ import java.util.Properties;
import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.internal.DefaultCacheKeysFactory;
import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cache.spi.CacheKeysFactory;
import org.hibernate.cache.spi.CollectionRegion;
import org.hibernate.cache.spi.EntityRegion;
import org.hibernate.cache.spi.NaturalIdRegion;
@ -28,19 +30,34 @@ public class CachingRegionFactory implements RegionFactory {
private static final Logger LOG = Logger.getLogger( CachingRegionFactory.class.getName() );
public static String DEFAULT_ACCESSTYPE = "DefaultAccessType";
private final CacheKeysFactory cacheKeysFactory;
private SessionFactoryOptions settings;
private Properties properties;
public CachingRegionFactory() {
LOG.warn( "CachingRegionFactory should be only used for testing." );
this( DefaultCacheKeysFactory.INSTANCE, null );
}
public CachingRegionFactory(CacheKeysFactory cacheKeysFactory) {
this( cacheKeysFactory, null );
}
public CachingRegionFactory(Properties properties) {
//add here to avoid run into catch
this( DefaultCacheKeysFactory.INSTANCE, properties );
}
public CachingRegionFactory(CacheKeysFactory cacheKeysFactory, Properties properties) {
LOG.warn( "CachingRegionFactory should be only used for testing." );
this.cacheKeysFactory = cacheKeysFactory;
this.properties = properties;
}
public CacheKeysFactory getCacheKeysFactory() {
return cacheKeysFactory;
}
@Override
public void start(SessionFactoryOptions settings, Properties properties) throws CacheException {
this.settings = settings;
@ -72,43 +89,42 @@ public class CachingRegionFactory implements RegionFactory {
@Override
public EntityRegion buildEntityRegion(String regionName, Properties properties, CacheDataDescription metadata)
throws CacheException {
return new EntityRegionImpl( regionName, metadata, settings );
return new EntityRegionImpl( this, regionName, metadata, settings );
}
@Override
public NaturalIdRegion buildNaturalIdRegion(String regionName, Properties properties, CacheDataDescription metadata)
throws CacheException {
return new NaturalIdRegionImpl( regionName, metadata, settings );
return new NaturalIdRegionImpl( this, regionName, metadata, settings );
}
@Override
public CollectionRegion buildCollectionRegion(
String regionName,
Properties properties,
CacheDataDescription metadata)
throws CacheException {
return new CollectionRegionImpl( regionName, metadata, settings );
CacheDataDescription metadata) throws CacheException {
return new CollectionRegionImpl( this, regionName, metadata, settings );
}
@Override
public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties properties) throws CacheException {
return new QueryResultsRegionImpl( regionName );
return new QueryResultsRegionImpl( this, regionName );
}
@Override
public TimestampsRegion buildTimestampsRegion(String regionName, Properties properties) throws CacheException {
return new TimestampsRegionImpl( regionName );
return new TimestampsRegionImpl( this, regionName );
}
private static class QueryResultsRegionImpl extends BaseGeneralDataRegion implements QueryResultsRegion {
QueryResultsRegionImpl(String name) {
super( name );
QueryResultsRegionImpl(CachingRegionFactory cachingRegionFactory, String name) {
super( cachingRegionFactory, name );
}
}
private static class TimestampsRegionImpl extends BaseGeneralDataRegion implements TimestampsRegion {
TimestampsRegionImpl(String name) {
super( name );
TimestampsRegionImpl(CachingRegionFactory cachingRegionFactory, String name) {
super( cachingRegionFactory, name );
}
}
}

View File

@ -23,8 +23,12 @@ class CollectionRegionImpl extends BaseTransactionalDataRegion implements Collec
private final SessionFactoryOptions settings;
CollectionRegionImpl(String name, CacheDataDescription metadata, SessionFactoryOptions settings) {
super( name, metadata );
CollectionRegionImpl(
CachingRegionFactory cachingRegionFactory,
String name,
CacheDataDescription metadata,
SessionFactoryOptions settings) {
super( cachingRegionFactory, name, metadata );
this.settings = settings;
}

View File

@ -18,14 +18,17 @@ import org.jboss.logging.Logger;
/**
* @author Strong Liu
*/
class EntityRegionImpl extends BaseTransactionalDataRegion implements EntityRegion {
public class EntityRegionImpl extends BaseTransactionalDataRegion implements EntityRegion {
private static final Logger LOG = Logger.getLogger( EntityRegionImpl.class );
private final SessionFactoryOptions settings;
EntityRegionImpl(String name, CacheDataDescription metadata, SessionFactoryOptions settings) {
super( name, metadata );
protected EntityRegionImpl(
CachingRegionFactory cachingRegionFactory,
String name,
CacheDataDescription metadata,
SessionFactoryOptions settings) {
super( cachingRegionFactory, name, metadata );
this.settings = settings;
}

View File

@ -23,8 +23,12 @@ class NaturalIdRegionImpl extends BaseTransactionalDataRegion implements Natural
private final SessionFactoryOptions settings;
NaturalIdRegionImpl(String name, CacheDataDescription metadata, SessionFactoryOptions settings) {
super( name, metadata );
NaturalIdRegionImpl(
CachingRegionFactory cachingRegionFactory,
String name,
CacheDataDescription metadata,
SessionFactoryOptions settings) {
super( cachingRegionFactory, name, metadata );
this.settings = settings;
}

View File

@ -48,11 +48,11 @@ class ReadWriteCollectionRegionAccessStrategy extends AbstractReadWriteAccessStr
@Override
public Object generateCacheKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
return DefaultCacheKeysFactory.createCollectionKey( id, persister, factory, tenantIdentifier );
return region.getRegionFactory().getCacheKeysFactory().createCollectionKey( id, persister, factory, tenantIdentifier );
}
@Override
public Object getCacheKeyId(Object cacheKey) {
return DefaultCacheKeysFactory.getCollectionId( cacheKey );
return region.getRegionFactory().getCacheKeysFactory().getCollectionId( cacheKey );
}
}

View File

@ -110,11 +110,11 @@ class ReadWriteEntityRegionAccessStrategy extends AbstractReadWriteAccessStrateg
@Override
public Object generateCacheKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
return DefaultCacheKeysFactory.createEntityKey( id, persister, factory, tenantIdentifier );
return region.getRegionFactory().getCacheKeysFactory().createEntityKey( id, persister, factory, tenantIdentifier );
}
@Override
public Object getCacheKeyId(Object cacheKey) {
return DefaultCacheKeysFactory.getEntityId( cacheKey );
return region.getRegionFactory().getCacheKeysFactory().getEntityId( cacheKey );
}
}

View File

@ -9,7 +9,6 @@ package org.hibernate.testing.cache;
import java.util.Comparator;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.internal.DefaultCacheKeysFactory;
import org.hibernate.cache.spi.NaturalIdRegion;
import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy;
import org.hibernate.cache.spi.access.SoftLock;
@ -107,11 +106,11 @@ class ReadWriteNaturalIdRegionAccessStrategy extends AbstractReadWriteAccessStra
@Override
public Object generateCacheKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) {
return DefaultCacheKeysFactory.createNaturalIdKey( naturalIdValues, persister, session );
return region.getRegionFactory().getCacheKeysFactory().createNaturalIdKey( naturalIdValues, persister, session );
}
@Override
public Object[] getNaturalIdValues(Object cacheKey) {
return DefaultCacheKeysFactory.getNaturalIdValues( cacheKey );
return region.getRegionFactory().getCacheKeysFactory().getNaturalIdValues( cacheKey );
}
}