Search in sources :

Example 16 with EntityRegionAccessStrategy

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

the class DefaultLoadEventListener method lockAndLoad.

/**
	 * If the class to be loaded has been configured with a cache, then lock
	 * given id in that cache and then perform the load.
	 *
	 * @param event The initiating load request event
	 * @param persister The persister corresponding to the entity to be loaded
	 * @param keyToLoad The key of the entity to be loaded
	 * @param options The defined load options
	 * @param source The originating session
	 *
	 * @return The loaded entity
	 *
	 * @throws HibernateException
	 */
private Object lockAndLoad(final LoadEvent event, final EntityPersister persister, final EntityKey keyToLoad, final LoadEventListener.LoadType options, final SessionImplementor source) {
    SoftLock lock = null;
    final Object ck;
    final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
    if (persister.hasCache()) {
        ck = cache.generateCacheKey(event.getEntityId(), persister, source.getFactory(), source.getTenantIdentifier());
        lock = persister.getCacheAccessStrategy().lockItem(source, ck, null);
    } else {
        ck = null;
    }
    Object entity;
    try {
        entity = load(event, persister, keyToLoad, options);
    } finally {
        if (persister.hasCache()) {
            cache.unlockItem(source, ck, lock);
        }
    }
    return event.getSession().getPersistenceContext().proxyFor(persister, keyToLoad, entity);
}
Also used : EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) SoftLock(org.hibernate.cache.spi.access.SoftLock)

Example 17 with EntityRegionAccessStrategy

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

the class DefaultLoadEventListener method getFromSharedCache.

private Object getFromSharedCache(final LoadEvent event, final EntityPersister persister, SessionImplementor source) {
    final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
    final Object ck = cache.generateCacheKey(event.getEntityId(), persister, source.getFactory(), source.getTenantIdentifier());
    final Object ce = CacheHelper.fromSharedCache(source, ck, persister.getCacheAccessStrategy());
    if (source.getFactory().getStatistics().isStatisticsEnabled()) {
        if (ce == null) {
            source.getFactory().getStatisticsImplementor().secondLevelCacheMiss(cache.getRegion().getName());
        } else {
            source.getFactory().getStatisticsImplementor().secondLevelCacheHit(cache.getRegion().getName());
        }
    }
    return ce;
}
Also used : EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy)

Example 18 with EntityRegionAccessStrategy

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

the class Loader method instanceNotYetLoaded.

/**
	 * The entity instance is not in the session cache
	 */
private Object instanceNotYetLoaded(final ResultSet rs, final int i, final Loadable persister, final String rowIdAlias, final EntityKey key, final LockMode lockMode, final EntityKey optionalObjectKey, final Object optionalObject, final List hydratedObjects, final SharedSessionContractImplementor session) throws HibernateException, SQLException {
    final String instanceClass = getInstanceClass(rs, i, persister, key.getIdentifier(), session);
    // see if the entity defines reference caching, and if so use the cached reference (if one).
    if (session.getCacheMode().isGetEnabled() && persister.canUseReferenceCacheEntries()) {
        final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
        final Object ck = cache.generateCacheKey(key.getIdentifier(), persister, session.getFactory(), session.getTenantIdentifier());
        final Object cachedEntry = CacheHelper.fromSharedCache(session, ck, cache);
        if (cachedEntry != null) {
            CacheEntry entry = (CacheEntry) persister.getCacheEntryStructure().destructure(cachedEntry, factory);
            return ((ReferenceCacheEntryImpl) entry).getReference();
        }
    }
    final Object object;
    if (optionalObjectKey != null && key.equals(optionalObjectKey)) {
        //its the given optional object
        object = optionalObject;
    } else {
        // instantiate a new instance
        object = session.instantiate(instanceClass, key.getIdentifier());
    }
    //need to hydrate it.
    // grab its state from the ResultSet and keep it in the Session
    // (but don't yet initialize the object itself)
    // note that we acquire LockMode.READ even if it was not requested
    LockMode acquiredLockMode = lockMode == LockMode.NONE ? LockMode.READ : lockMode;
    loadFromResultSet(rs, i, object, instanceClass, key, rowIdAlias, acquiredLockMode, persister, session);
    //materialize associations (and initialize the object) later
    hydratedObjects.add(object);
    return object;
}
Also used : EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) LockMode(org.hibernate.LockMode) CacheEntry(org.hibernate.cache.spi.entry.CacheEntry) ReferenceCacheEntryImpl(org.hibernate.cache.spi.entry.ReferenceCacheEntryImpl)

Example 19 with EntityRegionAccessStrategy

use of org.hibernate.cache.spi.access.EntityRegionAccessStrategy 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 EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
        SessionFactoryImplementor sessionFactoryImplementor = session.getFactory();
        final Object ck = cache.generateCacheKey(getId(), persister, sessionFactoryImplementor, session.getTenantIdentifier());
        final boolean put = cacheAfterInsert(cache, ck);
        if (put && sessionFactoryImplementor.getStatistics().isStatisticsEnabled()) {
            sessionFactoryImplementor.getStatisticsImplementor().secondLevelCachePut(cache.getRegion().getName());
        }
    }
    postCommitInsert(success);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor)

Example 20 with EntityRegionAccessStrategy

use of org.hibernate.cache.spi.access.EntityRegionAccessStrategy 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.hasCache()) {
        final EntityRegionAccessStrategy 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.hasCache()) {
        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().secondLevelCachePut(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) EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) CacheEntry(org.hibernate.cache.spi.entry.CacheEntry)

Aggregations

EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)24 EntityPersister (org.hibernate.persister.entity.EntityPersister)13 Serializable (java.io.Serializable)8 CollectionRegionAccessStrategy (org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)5 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 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)4 SoftLock (org.hibernate.cache.spi.access.SoftLock)3 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)3 Type (org.hibernate.type.Type)3 HibernateException (org.hibernate.HibernateException)2 LockMode (org.hibernate.LockMode)2 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)2 AssociationType (org.hibernate.type.AssociationType)2 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 EmbeddableType (javax.persistence.metamodel.EmbeddableType)1