Search in sources :

Example 1 with CachedNaturalIdValueSource

use of org.hibernate.engine.spi.CachedNaturalIdValueSource in project hibernate-orm by hibernate.

the class NaturalIdResolutionsImpl method manageSharedResolution.

private void manageSharedResolution(EntityPersister persister, final Object id, Object naturalIdValues, Object previousNaturalIdValues, CachedNaturalIdValueSource source) {
    final NaturalIdDataAccess cacheAccess = persister.getNaturalIdMapping().getCacheAccess();
    if (cacheAccess == null) {
        return;
    }
    final EntityMappingType rootEntityDescriptor = persister.getRootEntityDescriptor();
    final EntityPersister rootEntityPersister = rootEntityDescriptor.getEntityPersister();
    final Object cacheKey = cacheAccess.generateCacheKey(naturalIdValues, rootEntityPersister, session());
    final SessionFactoryImplementor factory = session().getFactory();
    final StatisticsImplementor statistics = factory.getStatistics();
    switch(source) {
        case LOAD:
            {
                if (CacheHelper.fromSharedCache(session(), cacheKey, cacheAccess) != null) {
                    // prevent identical re-cachings
                    return;
                }
                final boolean put = cacheAccess.putFromLoad(session(), cacheKey, id, null);
                if (put && statistics.isStatisticsEnabled()) {
                    statistics.naturalIdCachePut(rootEntityDescriptor.getNavigableRole(), cacheAccess.getRegion().getName());
                }
                break;
            }
        case INSERT:
            {
                final boolean put = cacheAccess.insert(session(), cacheKey, id);
                if (put && statistics.isStatisticsEnabled()) {
                    statistics.naturalIdCachePut(rootEntityDescriptor.getNavigableRole(), cacheAccess.getRegion().getName());
                }
                ((EventSource) session()).getActionQueue().registerProcess((success, session) -> {
                    if (success) {
                        final boolean put1 = cacheAccess.afterInsert(session, cacheKey, id);
                        if (put1 && statistics.isStatisticsEnabled()) {
                            statistics.naturalIdCachePut(rootEntityDescriptor.getNavigableRole(), cacheAccess.getRegion().getName());
                        }
                    } else {
                        cacheAccess.evict(cacheKey);
                    }
                });
                break;
            }
        case UPDATE:
            {
                final Object previousCacheKey = cacheAccess.generateCacheKey(previousNaturalIdValues, rootEntityPersister, session());
                if (cacheKey.equals(previousCacheKey)) {
                    // prevent identical re-caching, solves HHH-7309
                    return;
                }
                final SoftLock removalLock = cacheAccess.lockItem(session(), previousCacheKey, null);
                cacheAccess.remove(session(), previousCacheKey);
                final SoftLock lock = cacheAccess.lockItem(session(), cacheKey, null);
                final boolean put = cacheAccess.update(session(), cacheKey, id);
                if (put && statistics.isStatisticsEnabled()) {
                    statistics.naturalIdCachePut(rootEntityDescriptor.getNavigableRole(), cacheAccess.getRegion().getName());
                }
                ((EventSource) session()).getActionQueue().registerProcess((success, session) -> {
                    cacheAccess.unlockItem(session(), previousCacheKey, removalLock);
                    if (success) {
                        final boolean put12 = cacheAccess.afterUpdate(session(), cacheKey, id, lock);
                        if (put12 && statistics.isStatisticsEnabled()) {
                            statistics.naturalIdCachePut(rootEntityDescriptor.getNavigableRole(), cacheAccess.getRegion().getName());
                        }
                    } else {
                        cacheAccess.unlockItem(session(), cacheKey, lock);
                    }
                });
                break;
            }
        default:
            {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Unexpected CachedNaturalIdValueSource [" + source + "]");
                }
            }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) StatsHelper(org.hibernate.stat.internal.StatsHelper) AssertionFailure(org.hibernate.AssertionFailure) EntityPersister(org.hibernate.persister.entity.EntityPersister) Logger(org.jboss.logging.Logger) EventSource(org.hibernate.event.spi.EventSource) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) NaturalIdResolutions(org.hibernate.engine.spi.NaturalIdResolutions) NaturalIdLogging(org.hibernate.metamodel.mapping.NaturalIdLogging) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) ArrayList(java.util.ArrayList) Map(java.util.Map) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) LoadingLogger(org.hibernate.sql.results.LoadingLogger) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) Resolution(org.hibernate.engine.spi.Resolution) Serializable(java.io.Serializable) Objects(java.util.Objects) List(java.util.List) SoftLock(org.hibernate.cache.spi.access.SoftLock) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) NaturalIdDataAccess(org.hibernate.cache.spi.access.NaturalIdDataAccess) CachedNaturalIdValueSource(org.hibernate.engine.spi.CachedNaturalIdValueSource) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) EventSource(org.hibernate.event.spi.EventSource) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) NaturalIdDataAccess(org.hibernate.cache.spi.access.NaturalIdDataAccess) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) SoftLock(org.hibernate.cache.spi.access.SoftLock)

Aggregations

Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AssertionFailure (org.hibernate.AssertionFailure)1 NaturalIdDataAccess (org.hibernate.cache.spi.access.NaturalIdDataAccess)1 SoftLock (org.hibernate.cache.spi.access.SoftLock)1 CachedNaturalIdValueSource (org.hibernate.engine.spi.CachedNaturalIdValueSource)1 NaturalIdResolutions (org.hibernate.engine.spi.NaturalIdResolutions)1 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)1 Resolution (org.hibernate.engine.spi.Resolution)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)1 EventSource (org.hibernate.event.spi.EventSource)1 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)1 NaturalIdLogging (org.hibernate.metamodel.mapping.NaturalIdLogging)1 NaturalIdMapping (org.hibernate.metamodel.mapping.NaturalIdMapping)1