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 + "]");
}
}
}
}
Aggregations