Search in sources :

Example 11 with EntityRegionAccessStrategy

use of org.hibernate.cache.spi.access.EntityRegionAccessStrategy in project hibernate-orm by hibernate.

the class ConcurrentStatisticsImpl method getSecondLevelCacheStatistics.

/**
	 * Second level cache statistics per region
	 *
	 * @param regionName region name
	 *
	 * @return SecondLevelCacheStatistics
	 */
public ConcurrentSecondLevelCacheStatisticsImpl getSecondLevelCacheStatistics(String regionName) {
    ConcurrentSecondLevelCacheStatisticsImpl stat = secondLevelCacheStatistics.get(regionName);
    if (stat == null) {
        if (sessionFactory == null) {
            return null;
        }
        final EntityRegionAccessStrategy entityRegionAccess = sessionFactory.getCache().getEntityRegionAccess(regionName);
        final CollectionRegionAccessStrategy collectionRegionAccess = sessionFactory.getCache().getCollectionRegionAccess(regionName);
        if (entityRegionAccess == null && collectionRegionAccess == null) {
            final Region region = sessionFactory.getCache().getQueryCache(regionName).getRegion();
            if (region == null) {
                throw new IllegalArgumentException("Could not resolve region name [" + regionName + "]");
            }
            stat = new ConcurrentSecondLevelCacheStatisticsImpl(region, null, null);
        } else {
            final Region region = entityRegionAccess != null ? entityRegionAccess.getRegion() : collectionRegionAccess.getRegion();
            stat = new ConcurrentSecondLevelCacheStatisticsImpl(region, entityRegionAccess, collectionRegionAccess);
        }
        ConcurrentSecondLevelCacheStatisticsImpl previous;
        if ((previous = secondLevelCacheStatistics.putIfAbsent(regionName, stat)) != null) {
            stat = previous;
        }
    }
    return stat;
}
Also used : EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) Region(org.hibernate.cache.spi.Region) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)

Example 12 with EntityRegionAccessStrategy

use of org.hibernate.cache.spi.access.EntityRegionAccessStrategy in project hibernate-orm by hibernate.

the class SessionFactoryImplementor method getSecondLevelCacheRegionAccessStrategy.

/**
	 * Find the "access strategy" for the named cache region.
	 *
	 * @param regionName The name of the region
	 *
	 * @return That region's "access strategy"
	 *
	 *
	 * @deprecated (since 5.2) Use this factory's {@link #getCache()} reference
	 * to access {@link CacheImplementor#determineEntityRegionAccessStrategy} or
	 * {@link CacheImplementor#determineCollectionRegionAccessStrategy} instead.
	 */
@Deprecated
default default RegionAccessStrategy getSecondLevelCacheRegionAccessStrategy(String regionName) {
    final EntityRegionAccessStrategy entityRegionAccess = getCache().getEntityRegionAccess(regionName);
    if (entityRegionAccess != null) {
        return entityRegionAccess;
    }
    final CollectionRegionAccessStrategy collectionRegionAccess = getCache().getCollectionRegionAccess(regionName);
    if (collectionRegionAccess != null) {
        return collectionRegionAccess;
    }
    return null;
}
Also used : EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)

Example 13 with EntityRegionAccessStrategy

use of org.hibernate.cache.spi.access.EntityRegionAccessStrategy in project hibernate-orm by hibernate.

the class SessionFactoryImplementor method getSecondLevelCacheRegion.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Move to CacheImplementor calls
/**
	 * Get a named second-level cache region
	 *
	 * @param regionName The name of the region to retrieve.
	 *
	 * @return The name of the region
	 *
	 * @deprecated (since 5.2) Use this factory's {@link #getCache()} reference
	 * to access Region via {@link CacheImplementor#determineEntityRegionAccessStrategy} or
	 * {@link CacheImplementor#determineCollectionRegionAccessStrategy} instead.
	 */
@Deprecated
default default Region getSecondLevelCacheRegion(String regionName) {
    final EntityRegionAccessStrategy entityRegionAccess = getCache().getEntityRegionAccess(regionName);
    if (entityRegionAccess != null) {
        return entityRegionAccess.getRegion();
    }
    final CollectionRegionAccessStrategy collectionRegionAccess = getCache().getCollectionRegionAccess(regionName);
    if (collectionRegionAccess != null) {
        return collectionRegionAccess.getRegion();
    }
    return null;
}
Also used : EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)

Example 14 with EntityRegionAccessStrategy

use of org.hibernate.cache.spi.access.EntityRegionAccessStrategy in project hibernate-orm by hibernate.

the class DefaultLoadEventListener method lockAndLoad.

/**
	 * If the class to be loaded has been configured with a cache, then lock
	 * given id in that cache and then perform the load.
	 *
	 * @param event The initiating load request event
	 * @param persister The persister corresponding to the entity to be loaded
	 * @param keyToLoad The key of the entity to be loaded
	 * @param options The defined load options
	 * @param source The originating session
	 *
	 * @return The loaded entity
	 *
	 * @throws HibernateException
	 */
private Object lockAndLoad(final LoadEvent event, final EntityPersister persister, final EntityKey keyToLoad, final LoadEventListener.LoadType options, final SessionImplementor source) {
    SoftLock lock = null;
    final Object ck;
    final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
    if (persister.hasCache()) {
        ck = cache.generateCacheKey(event.getEntityId(), persister, source.getFactory(), source.getTenantIdentifier());
        lock = persister.getCacheAccessStrategy().lockItem(source, ck, null);
    } else {
        ck = null;
    }
    Object entity;
    try {
        entity = load(event, persister, keyToLoad, options);
    } finally {
        if (persister.hasCache()) {
            cache.unlockItem(source, ck, lock);
        }
    }
    return event.getSession().getPersistenceContext().proxyFor(persister, keyToLoad, entity);
}
Also used : EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) SoftLock(org.hibernate.cache.spi.access.SoftLock)

Example 15 with EntityRegionAccessStrategy

use of org.hibernate.cache.spi.access.EntityRegionAccessStrategy in project hibernate-orm by hibernate.

the class DefaultLoadEventListener method getFromSharedCache.

private Object getFromSharedCache(final LoadEvent event, final EntityPersister persister, SessionImplementor source) {
    final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
    final Object ck = cache.generateCacheKey(event.getEntityId(), persister, source.getFactory(), source.getTenantIdentifier());
    final Object ce = CacheHelper.fromSharedCache(source, ck, persister.getCacheAccessStrategy());
    if (source.getFactory().getStatistics().isStatisticsEnabled()) {
        if (ce == null) {
            source.getFactory().getStatisticsImplementor().secondLevelCacheMiss(cache.getRegion().getName());
        } else {
            source.getFactory().getStatisticsImplementor().secondLevelCacheHit(cache.getRegion().getName());
        }
    }
    return ce;
}
Also used : EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy)

Aggregations

EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)24 EntityPersister (org.hibernate.persister.entity.EntityPersister)13 Serializable (java.io.Serializable)8 CollectionRegionAccessStrategy (org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)5 CacheEntry (org.hibernate.cache.spi.entry.CacheEntry)5 EntityEntry (org.hibernate.engine.spi.EntityEntry)5 AssertionFailure (org.hibernate.AssertionFailure)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)4 SoftLock (org.hibernate.cache.spi.access.SoftLock)3 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)3 Type (org.hibernate.type.Type)3 HibernateException (org.hibernate.HibernateException)2 LockMode (org.hibernate.LockMode)2 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)2 AssociationType (org.hibernate.type.AssociationType)2 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 EmbeddableType (javax.persistence.metamodel.EmbeddableType)1