Search in sources :

Example 6 with LockMode

use of org.hibernate.LockMode in project hibernate-orm by hibernate.

the class QueryLoader method getLockModes.

/**
 * @param lockOptions a collection of lock modes specified dynamically via the Query interface
 */
@Override
protected LockMode[] getLockModes(LockOptions lockOptions) {
    if (lockOptions == null) {
        return defaultLockModes;
    }
    if (lockOptions.getAliasLockCount() == 0 && (lockOptions.getLockMode() == null || LockMode.NONE.equals(lockOptions.getLockMode()))) {
        return defaultLockModes;
    }
    // unfortunately this stuff can't be cached because
    // it is per-invocation, not constant for the
    // QueryTranslator instance
    LockMode[] lockModesArray = new LockMode[entityAliases.length];
    for (int i = 0; i < entityAliases.length; i++) {
        LockMode lockMode = lockOptions.getEffectiveLockMode(entityAliases[i]);
        if (lockMode == null) {
            // NONE, because its the requested lock mode, not the actual!
            lockMode = LockMode.NONE;
        }
        lockModesArray[i] = lockMode;
    }
    return lockModesArray;
}
Also used : LockMode(org.hibernate.LockMode)

Example 7 with LockMode

use of org.hibernate.LockMode in project hibernate-orm by hibernate.

the class QueryTranslatorImpl method getLockModes.

@Override
protected LockMode[] getLockModes(LockOptions lockOptions) {
    // unfortunately this stuff can't be cached because
    // it is per-invocation, not constant for the
    // QueryTranslator instance
    HashMap nameLockOptions = new HashMap();
    if (lockOptions == null) {
        lockOptions = LockOptions.NONE;
    }
    if (lockOptions.getAliasLockCount() > 0) {
        Iterator iter = lockOptions.getAliasLockIterator();
        while (iter.hasNext()) {
            Map.Entry me = (Map.Entry) iter.next();
            nameLockOptions.put(getAliasName((String) me.getKey()), me.getValue());
        }
    }
    LockMode[] lockModesArray = new LockMode[names.length];
    for (int i = 0; i < names.length; i++) {
        LockMode lm = (LockMode) nameLockOptions.get(names[i]);
        // if ( lm == null ) lm = LockOptions.NONE;
        if (lm == null) {
            lm = lockOptions.getLockMode();
        }
        lockModesArray[i] = lm;
    }
    return lockModesArray;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Iterator(java.util.Iterator) LockMode(org.hibernate.LockMode) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with LockMode

use of org.hibernate.LockMode in project hibernate-orm by hibernate.

the class EntityReferenceInitializerImpl method hydrateEntityState.

@Override
public void hydrateEntityState(ResultSet resultSet, ResultSetProcessingContextImpl context) {
    final EntityReferenceProcessingState processingState = context.getProcessingState(entityReference);
    // If there is no identifier for this entity reference for this row, nothing to do
    if (processingState.isMissingIdentifier()) {
        handleMissingIdentifier(context);
        return;
    }
    // make sure we have the EntityKey
    final EntityKey entityKey = processingState.getEntityKey();
    if (entityKey == null) {
        handleMissingIdentifier(context);
        return;
    }
    // Have we already hydrated this entity's state?
    if (processingState.getEntityInstance() != null) {
        return;
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // In getting here, we know that:
    // 1) We need to hydrate the entity state
    // 2) We have a valid EntityKey for the entity
    // see if we have an existing entry in the session for this EntityKey
    final Object existing = context.getSession().getEntityUsingInterceptor(entityKey);
    if (existing != null) {
        // It is previously associated with the Session, perform some checks
        if (!entityReference.getEntityPersister().isInstance(existing)) {
            throw new WrongClassException("loaded object was of wrong class " + existing.getClass(), entityKey.getIdentifier(), entityReference.getEntityPersister().getEntityName());
        }
        checkVersion(resultSet, context, entityKey, existing);
        // use the existing association as the hydrated state
        processingState.registerEntityInstance(existing);
        // context.registerHydratedEntity( entityReference, entityKey, existing );
        return;
    }
    // Otherwise, we need to load it from the ResultSet...
    // determine which entity instance to use.  Either the supplied one, or instantiate one
    Object entityInstance = null;
    if (isReturn && context.shouldUseOptionalEntityInformation() && context.getQueryParameters().getOptionalObject() != null) {
        final EntityKey optionalEntityKey = ResultSetProcessorHelper.getOptionalObjectKey(context.getQueryParameters(), context.getSession());
        if (optionalEntityKey != null && optionalEntityKey.equals(entityKey)) {
            entityInstance = context.getQueryParameters().getOptionalObject();
        }
    }
    final String concreteEntityTypeName = getConcreteEntityTypeName(resultSet, context, entityKey);
    if (entityInstance == null) {
        entityInstance = context.getSession().instantiate(concreteEntityTypeName, entityKey.getIdentifier());
    }
    processingState.registerEntityInstance(entityInstance);
    // 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
    log.trace("hydrating entity state");
    final LockMode requestedLockMode = context.resolveLockMode(entityReference);
    final LockMode lockModeToAcquire = requestedLockMode == LockMode.NONE ? LockMode.READ : requestedLockMode;
    loadFromResultSet(resultSet, context, entityInstance, concreteEntityTypeName, entityKey, lockModeToAcquire);
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) EntityReferenceProcessingState(org.hibernate.loader.plan.exec.process.spi.ResultSetProcessingContext.EntityReferenceProcessingState) LockMode(org.hibernate.LockMode) WrongClassException(org.hibernate.WrongClassException)

Example 9 with LockMode

use of org.hibernate.LockMode in project hibernate-orm by hibernate.

the class EntityReferenceInitializerImpl method checkVersion.

private void checkVersion(ResultSet resultSet, ResultSetProcessingContext context, EntityKey entityKey, Object existing) {
    final LockMode requestedLockMode = context.resolveLockMode(entityReference);
    if (requestedLockMode != LockMode.NONE) {
        final LockMode currentLockMode = context.getSession().getPersistenceContext().getEntry(existing).getLockMode();
        final boolean isVersionCheckNeeded = entityReference.getEntityPersister().isVersioned() && currentLockMode.lessThan(requestedLockMode);
        // by a re-entrant load (re-entrant loads *always* have lock mode NONE)
        if (isVersionCheckNeeded) {
            // we only check the version when *upgrading* lock modes
            checkVersion(context.getSession(), resultSet, entityReference.getEntityPersister(), entityReferenceAliases.getColumnAliases(), entityKey, existing);
            // we need to upgrade the lock mode to the mode requested
            context.getSession().getPersistenceContext().getEntry(existing).setLockMode(requestedLockMode);
        }
    }
}
Also used : LockMode(org.hibernate.LockMode)

Example 10 with LockMode

use of org.hibernate.LockMode in project hibernate-orm by hibernate.

the class AbstractLockUpgradeEventListener method upgradeLock.

/**
 * Performs a pessimistic lock upgrade on a given entity, if needed.
 *
 * @param object The entity for which to upgrade the lock.
 * @param entry The entity's EntityEntry instance.
 * @param lockOptions contains the requested lock mode.
 * @param source The session which is the source of the event being processed.
 */
protected void upgradeLock(Object object, EntityEntry entry, LockOptions lockOptions, EventSource source) {
    LockMode requestedLockMode = lockOptions.getLockMode();
    if (requestedLockMode.greaterThan(entry.getLockMode())) {
        if (entry.getStatus() != Status.MANAGED) {
            throw new ObjectDeletedException("attempted to lock a deleted instance", entry.getId(), entry.getPersister().getEntityName());
        }
        final EntityPersister persister = entry.getPersister();
        if (log.isTraceEnabled()) {
            log.tracev("Locking {0} in mode: {1}", MessageHelper.infoString(persister, entry.getId(), source.getFactory()), requestedLockMode);
        }
        final boolean cachingEnabled = persister.canWriteToCache();
        SoftLock lock = null;
        Object ck = null;
        try {
            if (cachingEnabled) {
                EntityDataAccess cache = persister.getCacheAccessStrategy();
                ck = cache.generateCacheKey(entry.getId(), persister, source.getFactory(), source.getTenantIdentifier());
                lock = cache.lockItem(source, ck, entry.getVersion());
            }
            if (persister.isVersioned() && requestedLockMode == LockMode.FORCE) {
                // todo : should we check the current isolation mode explicitly?
                Object nextVersion = persister.forceVersionIncrement(entry.getId(), entry.getVersion(), source);
                entry.forceLocked(object, nextVersion);
            } else {
                persister.lock(entry.getId(), entry.getVersion(), object, lockOptions, source);
            }
            entry.setLockMode(requestedLockMode);
        } finally {
            // so release the soft lock
            if (cachingEnabled) {
                persister.getCacheAccessStrategy().unlockItem(source, ck, lock);
            }
        }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ObjectDeletedException(org.hibernate.ObjectDeletedException) LockMode(org.hibernate.LockMode) SoftLock(org.hibernate.cache.spi.access.SoftLock) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess)

Aggregations

LockMode (org.hibernate.LockMode)25 Map (java.util.Map)9 HashMap (java.util.HashMap)8 LockOptions (org.hibernate.LockOptions)4 EntityPath (com.querydsl.core.types.EntityPath)3 Path (com.querydsl.core.types.Path)3 JPQLSerializer (com.querydsl.jpa.JPQLSerializer)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 Session (org.hibernate.Session)3 Query (org.hibernate.query.Query)3 Iterator (java.util.Iterator)2 List (java.util.List)2 Criteria (org.hibernate.Criteria)2 EntityDataAccess (org.hibernate.cache.spi.access.EntityDataAccess)2 EntityKey (org.hibernate.engine.spi.EntityKey)2 LoadQueryInfluencers (org.hibernate.engine.spi.LoadQueryInfluencers)2 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)2 EntityJoinWalker (org.hibernate.loader.entity.EntityJoinWalker)2 LoadQueryDetails (org.hibernate.loader.plan.exec.spi.LoadQueryDetails)2