Search in sources :

Example 1 with LockMode

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

the class EncapsulatedCompositeAttributeResultSetProcessorTest method doCompare.

private void doCompare(SessionFactoryImplementor sf, OuterJoinLoadable persister) {
    final LoadQueryInfluencers influencers = LoadQueryInfluencers.NONE;
    final LockMode lockMode = LockMode.NONE;
    final int batchSize = 1;
    final EntityJoinWalker walker = new EntityJoinWalker(persister, persister.getKeyColumnNames(), batchSize, lockMode, sf, influencers);
    final LoadQueryDetails details = Helper.INSTANCE.buildLoadQueryDetails(persister, sf);
    compare(walker, details);
}
Also used : LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) LoadQueryDetails(org.hibernate.loader.plan.exec.spi.LoadQueryDetails) EntityJoinWalker(org.hibernate.loader.entity.EntityJoinWalker) LockMode(org.hibernate.LockMode)

Example 2 with LockMode

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

the class AbstractTransactSQLDialect method applyLocksToSql.

@Override
public String applyLocksToSql(String sql, LockOptions aliasedLockOptions, Map<String, String[]> keyColumnNames) {
    // TODO:  merge additional lockoptions support in Dialect.applyLocksToSql
    final Iterator itr = aliasedLockOptions.getAliasLockIterator();
    final StringBuilder buffer = new StringBuilder(sql);
    while (itr.hasNext()) {
        final Map.Entry entry = (Map.Entry) itr.next();
        final LockMode lockMode = (LockMode) entry.getValue();
        if (lockMode.greaterThan(LockMode.READ)) {
            final String alias = (String) entry.getKey();
            int start = -1;
            int end = -1;
            if (sql.endsWith(" " + alias)) {
                start = (buffer.length() - alias.length());
                end = start + alias.length();
            } else {
                int position = buffer.indexOf(" " + alias + " ");
                if (position <= -1) {
                    position = buffer.indexOf(" " + alias + ",");
                }
                if (position > -1) {
                    start = position + 1;
                    end = start + alias.length();
                }
            }
            if (start > -1) {
                final String lockHint = appendLockHint(aliasedLockOptions, alias);
                buffer.replace(start, end, lockHint);
            }
        }
    }
    return buffer.toString();
}
Also used : Iterator(java.util.Iterator) LockMode(org.hibernate.LockMode) Map(java.util.Map)

Example 3 with LockMode

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

the class Loader method shouldUseFollowOnLocking.

protected boolean shouldUseFollowOnLocking(QueryParameters parameters, Dialect dialect, List<AfterLoadAction> afterLoadActions) {
    if ((parameters.getLockOptions().getFollowOnLocking() == null && dialect.useFollowOnLocking(parameters)) || (parameters.getLockOptions().getFollowOnLocking() != null && parameters.getLockOptions().getFollowOnLocking())) {
        // currently only one lock mode is allowed in follow-on locking
        final LockMode lockMode = determineFollowOnLockMode(parameters.getLockOptions());
        final LockOptions lockOptions = new LockOptions(lockMode);
        if (lockOptions.getLockMode() != LockMode.UPGRADE_SKIPLOCKED) {
            if (lockOptions.getLockMode() != LockMode.NONE) {
                LOG.usingFollowOnLocking();
            }
            lockOptions.setTimeOut(parameters.getLockOptions().getTimeOut());
            lockOptions.setScope(parameters.getLockOptions().getScope());
            afterLoadActions.add(new AfterLoadAction() {

                @Override
                public void afterLoad(SharedSessionContractImplementor session, Object entity, Loadable persister) {
                    ((Session) session).buildLockRequest(lockOptions).lock(persister.getEntityName(), entity);
                }
            });
            parameters.setLockOptions(new LockOptions());
            return true;
        }
    }
    return false;
}
Also used : UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable) Loadable(org.hibernate.persister.entity.Loadable) LockOptions(org.hibernate.LockOptions) AfterLoadAction(org.hibernate.loader.spi.AfterLoadAction) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) LockMode(org.hibernate.LockMode) Session(org.hibernate.Session)

Example 4 with LockMode

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

the class CriteriaLoader method getLockModes.

@Override
protected LockMode[] getLockModes(LockOptions lockOptions) {
    final String[] entityAliases = getAliases();
    if (entityAliases == null) {
        return null;
    }
    final int size = entityAliases.length;
    LockMode[] lockModesArray = new LockMode[size];
    for (int i = 0; i < size; i++) {
        LockMode lockMode = lockOptions.getAliasSpecificLockMode(entityAliases[i]);
        lockModesArray[i] = lockMode == null ? lockOptions.getLockMode() : lockMode;
    }
    return lockModesArray;
}
Also used : LockMode(org.hibernate.LockMode)

Example 5 with LockMode

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

the class Loader method processResultSet.

protected List processResultSet(ResultSet rs, QueryParameters queryParameters, SharedSessionContractImplementor session, boolean returnProxies, ResultTransformer forcedResultTransformer, int maxRows, List<AfterLoadAction> afterLoadActions) throws SQLException {
    final int entitySpan = getEntityPersisters().length;
    final EntityKey optionalObjectKey = getOptionalObjectKey(queryParameters, session);
    final LockMode[] lockModesArray = getLockModes(queryParameters.getLockOptions());
    final boolean createSubselects = isSubselectLoadingEnabled();
    final List subselectResultKeys = createSubselects ? new ArrayList() : null;
    final ArrayList hydratedObjects = entitySpan == 0 ? null : new ArrayList(entitySpan * 10);
    final List results = new ArrayList();
    handleEmptyCollections(queryParameters.getCollectionKeys(), rs, session);
    // we can reuse it for each row
    EntityKey[] keys = new EntityKey[entitySpan];
    LOG.trace("Processing result set");
    int count;
    for (count = 0; count < maxRows && rs.next(); count++) {
        if (DEBUG_ENABLED) {
            LOG.debugf("Result set row: %s", count);
        }
        Object result = getRowFromResultSet(rs, session, queryParameters, lockModesArray, optionalObjectKey, hydratedObjects, keys, returnProxies, forcedResultTransformer);
        results.add(result);
        if (createSubselects) {
            subselectResultKeys.add(keys);
            // can't reuse in this case
            keys = new EntityKey[entitySpan];
        }
    }
    LOG.tracev("Done processing result set ({0} rows)", count);
    initializeEntitiesAndCollections(hydratedObjects, rs, session, queryParameters.isReadOnly(session), afterLoadActions);
    if (createSubselects) {
        createSubselects(subselectResultKeys, queryParameters, session);
    }
    return results;
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) LockMode(org.hibernate.LockMode)

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