Search in sources :

Example 6 with CollectionRegionAccessStrategy

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

the class CacheImpl method close.

@Override
public void close() {
    for (EntityRegionAccessStrategy access : entityRegionAccessStrategyMap.values()) {
        access.getRegion().destroy();
    }
    for (CollectionRegionAccessStrategy access : collectionRegionAccessStrategyMap.values()) {
        access.getRegion().destroy();
    }
    if (settings.isQueryCacheEnabled()) {
        defaultQueryCache.destroy();
        for (QueryCache cache : queryCaches.values()) {
            cache.destroy();
        }
        updateTimestampsCache.destroy();
    }
    regionFactory.stop();
}
Also used : QueryCache(org.hibernate.cache.spi.QueryCache) StandardQueryCache(org.hibernate.cache.internal.StandardQueryCache) EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)

Example 7 with CollectionRegionAccessStrategy

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

the class CacheImpl method containsCollection.

@Override
public boolean containsCollection(String role, Serializable ownerIdentifier) {
    CollectionPersister p = sessionFactory.getMetamodel().collectionPersister(role);
    if (p.hasCache()) {
        CollectionRegionAccessStrategy cache = p.getCacheAccessStrategy();
        // have to assume non tenancy
        Object key = cache.generateCacheKey(ownerIdentifier, p, sessionFactory, null);
        return cache.getRegion().contains(key);
    } else {
        return false;
    }
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)

Example 8 with CollectionRegionAccessStrategy

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

the class CacheImpl method evictCollection.

@Override
public void evictCollection(String role, Serializable ownerIdentifier) {
    CollectionPersister p = sessionFactory.getMetamodel().collectionPersister(role);
    if (p.hasCache()) {
        if (LOG.isDebugEnabled()) {
            LOG.debugf("Evicting second-level cache: %s", MessageHelper.collectionInfoString(p, ownerIdentifier, sessionFactory));
        }
        CollectionRegionAccessStrategy cache = p.getCacheAccessStrategy();
        // have to assume non tenancy
        Object key = cache.generateCacheKey(ownerIdentifier, p, sessionFactory, null);
        cache.evict(key);
    }
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)

Example 9 with CollectionRegionAccessStrategy

use of org.hibernate.cache.spi.access.CollectionRegionAccessStrategy 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 10 with CollectionRegionAccessStrategy

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

the class CollectionCacheEvictionTest method testCachedValueAfterEviction.

@Test
public void testCachedValueAfterEviction() {
    CollectionPersister persister = sessionFactory().getCollectionPersister(Company.class.getName() + ".users");
    Session session = openSession();
    SessionImplementor sessionImplementor = (SessionImplementor) session;
    CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy();
    Object key = cache.generateCacheKey(1, persister, sessionFactory(), session.getTenantIdentifier());
    Object cachedValue = cache.get(sessionImplementor, key, sessionImplementor.getTimestamp());
    assertNull(cachedValue);
    Company company = session.get(Company.class, 1);
    //should add in cache
    assertEquals(1, company.getUsers().size());
    session.close();
    session = openSession();
    sessionImplementor = (SessionImplementor) session;
    key = cache.generateCacheKey(1, persister, sessionFactory(), session.getTenantIdentifier());
    cachedValue = cache.get(sessionImplementor, key, sessionImplementor.getTimestamp());
    assertNotNull("Collection wasn't cached", cachedValue);
    session.close();
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

CollectionRegionAccessStrategy (org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)18 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)7 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)5 Session (org.hibernate.Session)3 CollectionCacheEntry (org.hibernate.cache.spi.entry.CollectionCacheEntry)3 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)3 HibernateException (org.hibernate.HibernateException)2 CollectionRegion (org.hibernate.cache.spi.CollectionRegion)2 SoftLock (org.hibernate.cache.spi.access.SoftLock)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 Type (org.hibernate.type.Type)2 Test (org.junit.Test)2 Serializable (java.io.Serializable)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 EmbeddableType (javax.persistence.metamodel.EmbeddableType)1 EntityType (javax.persistence.metamodel.EntityType)1