Search in sources :

Example 16 with LockMode

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

the class PostgreSQL81Dialect method getForUpdateString.

@Override
public String getForUpdateString(String aliases, LockOptions lockOptions) {
    /*
		 * Parent's implementation for (aliases, lockOptions) ignores aliases.
		 */
    if ("".equals(aliases)) {
        LockMode lockMode = lockOptions.getLockMode();
        final Iterator<Map.Entry<String, LockMode>> itr = lockOptions.getAliasLockIterator();
        while (itr.hasNext()) {
            // seek the highest lock mode
            final Map.Entry<String, LockMode> entry = itr.next();
            final LockMode lm = entry.getValue();
            if (lm.greaterThan(lockMode)) {
                aliases = entry.getKey();
            }
        }
    }
    LockMode lockMode = lockOptions.getAliasSpecificLockMode(aliases);
    if (lockMode == null) {
        lockMode = lockOptions.getLockMode();
    }
    switch(lockMode) {
        case UPGRADE:
            return getForUpdateString(aliases);
        case PESSIMISTIC_READ:
            return getReadLockString(aliases, lockOptions.getTimeOut());
        case PESSIMISTIC_WRITE:
            return getWriteLockString(aliases, lockOptions.getTimeOut());
        case UPGRADE_NOWAIT:
        case FORCE:
        case PESSIMISTIC_FORCE_INCREMENT:
            return getForUpdateNowaitString(aliases);
        case UPGRADE_SKIPLOCKED:
            return getForUpdateSkipLockedString(aliases);
        default:
            return "";
    }
}
Also used : LockMode(org.hibernate.LockMode) Map(java.util.Map)

Example 17 with LockMode

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

the class Dialect method getForUpdateString.

/**
 * Get the <tt>FOR UPDATE OF column_list</tt> fragment appropriate for this
 * dialect given the aliases of the columns to be write locked.
 *
 * @param aliases The columns to be write locked.
 * @param lockOptions the lock options to apply
 * @return The appropriate <tt>FOR UPDATE OF column_list</tt> clause string.
 */
@SuppressWarnings({ "unchecked", "UnusedParameters" })
public String getForUpdateString(String aliases, LockOptions lockOptions) {
    LockMode lockMode = lockOptions.getLockMode();
    final Iterator<Map.Entry<String, LockMode>> itr = lockOptions.getAliasLockIterator();
    while (itr.hasNext()) {
        // seek the highest lock mode
        final Map.Entry<String, LockMode> entry = itr.next();
        final LockMode lm = entry.getValue();
        if (lm.greaterThan(lockMode)) {
            lockMode = lm;
        }
    }
    lockOptions.setLockMode(lockMode);
    return getForUpdateString(lockOptions);
}
Also used : LockMode(org.hibernate.LockMode) Map(java.util.Map) HashMap(java.util.HashMap)

Example 18 with LockMode

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

the class AbstractHANADialect method getForUpdateString.

@Override
public String getForUpdateString(final String aliases, final LockOptions lockOptions) {
    LockMode lockMode = lockOptions.findGreatestLockMode();
    lockOptions.setLockMode(lockMode);
    // not sure why this is sometimes empty
    if (aliases == null || aliases.isEmpty()) {
        return getForUpdateString(lockOptions);
    }
    return getForUpdateString(aliases, lockMode, lockOptions.getTimeOut());
}
Also used : LockMode(org.hibernate.LockMode)

Example 19 with LockMode

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

the class SQLServer2005Dialect method appendLockHint.

@Override
public String appendLockHint(LockOptions lockOptions, String tableName) {
    LockMode lockMode = lockOptions.getAliasSpecificLockMode(tableName);
    if (lockMode == null) {
        lockMode = lockOptions.getLockMode();
    }
    final String writeLockStr = lockOptions.getTimeOut() == LockOptions.SKIP_LOCKED ? "updlock" : "updlock, holdlock";
    final String readLockStr = lockOptions.getTimeOut() == LockOptions.SKIP_LOCKED ? "updlock" : "holdlock";
    final String noWaitStr = lockOptions.getTimeOut() == LockOptions.NO_WAIT ? ", nowait" : "";
    final String skipLockStr = lockOptions.getTimeOut() == LockOptions.SKIP_LOCKED ? ", readpast" : "";
    switch(lockMode) {
        case UPGRADE:
        case PESSIMISTIC_WRITE:
        case WRITE:
            {
                return tableName + " with (" + writeLockStr + ", rowlock" + noWaitStr + skipLockStr + ")";
            }
        case PESSIMISTIC_READ:
            {
                return tableName + " with (" + readLockStr + ", rowlock" + noWaitStr + skipLockStr + ")";
            }
        case UPGRADE_SKIPLOCKED:
            return tableName + " with (updlock, rowlock, readpast" + noWaitStr + ")";
        case UPGRADE_NOWAIT:
            return tableName + " with (updlock, holdlock, rowlock, nowait)";
        default:
            {
                return tableName;
            }
    }
}
Also used : LockMode(org.hibernate.LockMode)

Example 20 with LockMode

use of org.hibernate.LockMode 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 EntityDataAccess 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 : LockMode(org.hibernate.LockMode) CacheEntry(org.hibernate.cache.spi.entry.CacheEntry) ReferenceCacheEntryImpl(org.hibernate.cache.spi.entry.ReferenceCacheEntryImpl) 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