use of org.hibernate.cache.spi.access.EntityDataAccess in project hibernate-orm by hibernate.
the class EntityDeleteAction method doAfterTransactionCompletion.
@Override
public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) throws HibernateException {
EntityPersister entityPersister = getPersister();
if (entityPersister.canWriteToCache()) {
EntityDataAccess cache = entityPersister.getCacheAccessStrategy();
final Object ck = cache.generateCacheKey(getId(), entityPersister, session.getFactory(), session.getTenantIdentifier());
cache.unlockItem(session, ck, lock);
}
postCommitDelete(success);
}
use of org.hibernate.cache.spi.access.EntityDataAccess in project hibernate-orm by hibernate.
the class EntityUpdateAction method execute.
@Override
public void execute() throws HibernateException {
final Serializable id = getId();
final EntityPersister persister = getPersister();
final SharedSessionContractImplementor session = getSession();
final Object instance = getInstance();
final boolean veto = preUpdate();
final SessionFactoryImplementor factory = session.getFactory();
Object previousVersion = this.previousVersion;
if (persister.isVersionPropertyGenerated()) {
// we need to grab the version value from the entity, otherwise
// we have issues with generated-version entities that may have
// multiple actions queued during the same flush
previousVersion = persister.getVersion(instance);
}
final Object ck;
if (persister.canWriteToCache()) {
final EntityDataAccess cache = persister.getCacheAccessStrategy();
ck = cache.generateCacheKey(id, persister, factory, session.getTenantIdentifier());
lock = cache.lockItem(session, ck, previousVersion);
} else {
ck = null;
}
if (!veto) {
persister.update(id, state, dirtyFields, hasDirtyCollection, previousState, previousVersion, instance, rowId, session);
}
final EntityEntry entry = session.getPersistenceContext().getEntry(instance);
if (entry == null) {
throw new AssertionFailure("possible nonthreadsafe access to session");
}
if (entry.getStatus() == Status.MANAGED || persister.isVersionPropertyGenerated()) {
// get the updated snapshot of the entity state by cloning current state;
// it is safe to copy in place, since by this time no-one else (should have)
// has a reference to the array
TypeHelper.deepCopy(state, persister.getPropertyTypes(), persister.getPropertyCheckability(), state, session);
if (persister.hasUpdateGeneratedProperties()) {
// this entity defines proeprty generation, so process those generated
// values...
persister.processUpdateGeneratedProperties(id, instance, state, session);
if (persister.isVersionPropertyGenerated()) {
nextVersion = Versioning.getVersion(state, persister);
}
}
// have the entity entry doAfterTransactionCompletion post-update processing, passing it the
// update state and the new version (if one).
entry.postUpdate(instance, state, nextVersion);
}
if (persister.canWriteToCache()) {
if (persister.isCacheInvalidationRequired() || entry.getStatus() != Status.MANAGED) {
persister.getCacheAccessStrategy().remove(session, ck);
} else if (session.getCacheMode().isPutEnabled()) {
// TODO: inefficient if that cache is just going to ignore the updated state!
final CacheEntry ce = persister.buildCacheEntry(instance, state, nextVersion, getSession());
cacheEntry = persister.getCacheEntryStructure().structure(ce);
final boolean put = cacheUpdate(persister, previousVersion, ck);
if (put && factory.getStatistics().isStatisticsEnabled()) {
factory.getStatistics().entityCachePut(StatsHelper.INSTANCE.getRootEntityRole(persister), getPersister().getCacheAccessStrategy().getRegion().getName());
}
}
}
session.getPersistenceContext().getNaturalIdHelper().manageSharedNaturalIdCrossReference(persister, id, state, previousNaturalIdValues, CachedNaturalIdValueSource.UPDATE);
postUpdate();
if (factory.getStatistics().isStatisticsEnabled() && !veto) {
factory.getStatistics().updateEntity(getPersister().getEntityName());
}
}
use of org.hibernate.cache.spi.access.EntityDataAccess in project hibernate-orm by hibernate.
the class EntityUpdateAction method doAfterTransactionCompletion.
@Override
public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) throws CacheException {
final EntityPersister persister = getPersister();
if (persister.canWriteToCache()) {
final EntityDataAccess cache = persister.getCacheAccessStrategy();
final Object ck = cache.generateCacheKey(getId(), persister, session.getFactory(), session.getTenantIdentifier());
if (success && cacheEntry != null && !persister.isCacheInvalidationRequired() && session.getCacheMode().isPutEnabled()) {
final boolean put = cacheAfterUpdate(cache, ck);
if (put && getSession().getFactory().getStatistics().isStatisticsEnabled()) {
session.getFactory().getStatistics().entityCachePut(StatsHelper.INSTANCE.getRootEntityRole(persister), getPersister().getCacheAccessStrategy().getRegion().getName());
}
} else {
cache.unlockItem(session, ck, lock);
}
}
postCommitUpdate(success);
}
use of org.hibernate.cache.spi.access.EntityDataAccess in project hibernate-orm by hibernate.
the class EntityInsertAction method doAfterTransactionCompletion.
@Override
public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) throws HibernateException {
final EntityPersister persister = getPersister();
if (success && isCachePutEnabled(persister, getSession())) {
final EntityDataAccess cache = persister.getCacheAccessStrategy();
SessionFactoryImplementor factory = session.getFactory();
final Object ck = cache.generateCacheKey(getId(), persister, factory, session.getTenantIdentifier());
final boolean put = cacheAfterInsert(cache, ck);
if (put && factory.getStatistics().isStatisticsEnabled()) {
factory.getStatistics().entityCachePut(StatsHelper.INSTANCE.getRootEntityRole(persister), cache.getRegion().getName());
}
}
postCommitInsert(success);
}
use of org.hibernate.cache.spi.access.EntityDataAccess in project hibernate-orm by hibernate.
the class StatelessSessionImpl method refresh.
@Override
public void refresh(String entityName, Object entity, LockMode lockMode) {
final EntityPersister persister = this.getEntityPersister(entityName, entity);
final Serializable id = persister.getIdentifier(entity, this);
if (LOG.isTraceEnabled()) {
LOG.tracev("Refreshing transient {0}", MessageHelper.infoString(persister, id, this.getFactory()));
}
if (persister.canWriteToCache()) {
final EntityDataAccess cacheAccess = persister.getCacheAccessStrategy();
if (cacheAccess != null) {
final Object ck = cacheAccess.generateCacheKey(id, persister, getFactory(), getTenantIdentifier());
cacheAccess.evict(ck);
}
}
String previousFetchProfile = this.getLoadQueryInfluencers().getInternalFetchProfile();
Object result = null;
try {
this.getLoadQueryInfluencers().setInternalFetchProfile("refresh");
result = persister.load(id, entity, getNullSafeLockMode(lockMode), this);
} finally {
this.getLoadQueryInfluencers().setInternalFetchProfile(previousFetchProfile);
}
UnresolvableObjectException.throwIfNull(result, id, persister.getEntityName());
}
Aggregations