Search in sources :

Example 16 with EntityDataAccess

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);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess)

Example 17 with EntityDataAccess

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());
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Serializable(java.io.Serializable) EntityEntry(org.hibernate.engine.spi.EntityEntry) AssertionFailure(org.hibernate.AssertionFailure) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) CacheEntry(org.hibernate.cache.spi.entry.CacheEntry) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess)

Example 18 with EntityDataAccess

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);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess)

Example 19 with EntityDataAccess

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);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess)

Example 20 with EntityDataAccess

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());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Serializable(java.io.Serializable) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess)

Aggregations

EntityDataAccess (org.hibernate.cache.spi.access.EntityDataAccess)24 EntityPersister (org.hibernate.persister.entity.EntityPersister)15 Serializable (java.io.Serializable)8 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 Map (java.util.Map)3 HibernateException (org.hibernate.HibernateException)3 DomainDataRegion (org.hibernate.cache.spi.DomainDataRegion)3 SoftLock (org.hibernate.cache.spi.access.SoftLock)3 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)3 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)3 Test (org.junit.Test)3 CoreMatchers.instanceOf (org.hamcrest.CoreMatchers.instanceOf)2 CoreMatchers.is (org.hamcrest.CoreMatchers.is)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 LockMode (org.hibernate.LockMode)2 Session (org.hibernate.Session)2 CollectionDataAccess (org.hibernate.cache.spi.access.CollectionDataAccess)2