Search in sources :

Example 21 with LockMode

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

the class AbstractProducedQuery method setHint.

@Override
@SuppressWarnings("unchecked")
public QueryImplementor setHint(String hintName, Object value) {
    getProducer().checkOpen(true);
    boolean applied = false;
    try {
        if (HINT_TIMEOUT.equals(hintName)) {
            applied = applyTimeoutHint(ConfigurationHelper.getInteger(value));
        } else if (SPEC_HINT_TIMEOUT.equals(hintName)) {
            // convert milliseconds to seconds
            int timeout = (int) Math.round(ConfigurationHelper.getInteger(value).doubleValue() / 1000.0);
            applied = applyTimeoutHint(timeout);
        } else if (JPA_LOCK_TIMEOUT.equals(hintName)) {
            applied = applyLockTimeoutHint(ConfigurationHelper.getInteger(value));
        } else if (HINT_COMMENT.equals(hintName)) {
            applied = applyCommentHint((String) value);
        } else if (HINT_FETCH_SIZE.equals(hintName)) {
            applied = applyFetchSizeHint(ConfigurationHelper.getInteger(value));
        } else if (HINT_CACHEABLE.equals(hintName)) {
            applied = applyCacheableHint(ConfigurationHelper.getBoolean(value));
        } else if (HINT_CACHE_REGION.equals(hintName)) {
            applied = applyCacheRegionHint((String) value);
        } else if (HINT_READONLY.equals(hintName)) {
            applied = applyReadOnlyHint(ConfigurationHelper.getBoolean(value));
        } else if (HINT_FLUSH_MODE.equals(hintName)) {
            applied = applyFlushModeHint(ConfigurationHelper.getFlushMode(value));
        } else if (HINT_CACHE_MODE.equals(hintName)) {
            applied = applyCacheModeHint(ConfigurationHelper.getCacheMode(value));
        } else if (JPA_SHARED_CACHE_RETRIEVE_MODE.equals(hintName)) {
            final CacheRetrieveMode retrieveMode = value != null ? CacheRetrieveMode.valueOf(value.toString()) : null;
            applied = applyJpaCacheRetrieveMode(retrieveMode);
        } else if (JPA_SHARED_CACHE_STORE_MODE.equals(hintName)) {
            final CacheStoreMode storeMode = value != null ? CacheStoreMode.valueOf(value.toString()) : null;
            applied = applyJpaCacheStoreMode(storeMode);
        } else if (QueryHints.HINT_NATIVE_LOCKMODE.equals(hintName)) {
            applied = applyNativeQueryLockMode(value);
        } else if (hintName.startsWith(ALIAS_SPECIFIC_LOCK_MODE)) {
            if (canApplyAliasSpecificLockModeHints()) {
                // extract the alias
                final String alias = hintName.substring(ALIAS_SPECIFIC_LOCK_MODE.length() + 1);
                // determine the LockMode
                try {
                    final LockMode lockMode = LockModeTypeHelper.interpretLockMode(value);
                    applyAliasSpecificLockModeHint(alias, lockMode);
                } catch (Exception e) {
                    MSG_LOGGER.unableToDetermineLockModeValue(hintName, value);
                    applied = false;
                }
            } else {
                applied = false;
            }
        } else if (HINT_FETCHGRAPH.equals(hintName) || HINT_LOADGRAPH.equals(hintName)) {
            if (value instanceof EntityGraphImpl) {
                applyEntityGraphQueryHint(new EntityGraphQueryHint(hintName, (EntityGraphImpl) value));
            } else {
                MSG_LOGGER.warnf("The %s hint was set, but the value was not an EntityGraph!", hintName);
            }
            applied = true;
        } else if (HINT_FOLLOW_ON_LOCKING.equals(hintName)) {
            applied = applyFollowOnLockingHint(ConfigurationHelper.getBoolean(value));
        } else if (QueryHints.HINT_PASS_DISTINCT_THROUGH.equals(hintName)) {
            applied = applyPassDistinctThrough(ConfigurationHelper.getBoolean(value));
        } else {
            MSG_LOGGER.ignoringUnrecognizedQueryHint(hintName);
        }
    } catch (ClassCastException e) {
        throw new IllegalArgumentException("Value for hint");
    }
    if (!applied) {
        MSG_LOGGER.debugf("Skipping unsupported query hint [%s]", hintName);
    }
    return this;
}
Also used : EntityGraphImpl(org.hibernate.jpa.graph.internal.EntityGraphImpl) CacheRetrieveMode(javax.persistence.CacheRetrieveMode) EntityGraphQueryHint(org.hibernate.engine.query.spi.EntityGraphQueryHint) LockMode(org.hibernate.LockMode) CacheStoreMode(javax.persistence.CacheStoreMode) NoResultException(javax.persistence.NoResultException) NonUniqueResultException(org.hibernate.NonUniqueResultException) TransactionRequiredException(javax.persistence.TransactionRequiredException) HibernateException(org.hibernate.HibernateException) TypeMismatchException(org.hibernate.TypeMismatchException) QueryParameterException(org.hibernate.QueryParameterException) PropertyNotFoundException(org.hibernate.PropertyNotFoundException) QueryExecutionRequestException(org.hibernate.hql.internal.QueryExecutionRequestException)

Example 22 with LockMode

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

the class QueryLoader method applyLocks.

@Override
protected String applyLocks(String sql, QueryParameters parameters, Dialect dialect, List<AfterLoadAction> afterLoadActions) throws QueryException {
    // can't cache this stuff either (per-invocation)
    // we are given a map of user-alias -> lock mode
    // create a new map of sql-alias -> lock mode
    final LockOptions lockOptions = parameters.getLockOptions();
    if (lockOptions == null || (lockOptions.getLockMode() == LockMode.NONE && lockOptions.getAliasLockCount() == 0)) {
        return sql;
    }
    // some dialects wont allow locking with paging...
    if (shouldUseFollowOnLocking(parameters, dialect, afterLoadActions)) {
        return sql;
    }
    // there are other conditions we might want to add here, such as checking the result types etc
    // but those are better served after we have redone the SQL generation to use ASTs.
    // we need both the set of locks and the columns to reference in locks
    // as the ultimate output of this section...
    final LockOptions locks = new LockOptions(lockOptions.getLockMode());
    final Map<String, String[]> keyColumnNames = dialect.forUpdateOfColumns() ? new HashMap<>() : null;
    locks.setScope(lockOptions.getScope());
    locks.setTimeOut(lockOptions.getTimeOut());
    for (Map.Entry<String, String> entry : sqlAliasByEntityAlias.entrySet()) {
        final String userAlias = entry.getKey();
        final String drivingSqlAlias = entry.getValue();
        if (drivingSqlAlias == null) {
            throw new IllegalArgumentException("could not locate alias to apply lock mode : " + userAlias);
        }
        // at this point we have (drivingSqlAlias) the SQL alias of the driving table
        // corresponding to the given user alias.  However, the driving table is not
        // (necessarily) the table against which we want to apply locks.  Mainly,
        // the exception case here is joined-subclass hierarchies where we instead
        // want to apply the lock against the root table (for all other strategies,
        // it just happens that driving and root are the same).
        final QueryNode select = (QueryNode) queryTranslator.getSqlAST();
        final Lockable drivingPersister = (Lockable) select.getFromClause().findFromElementByUserOrSqlAlias(userAlias, drivingSqlAlias).getQueryable();
        final String sqlAlias = drivingPersister.getRootTableAlias(drivingSqlAlias);
        final LockMode effectiveLockMode = lockOptions.getEffectiveLockMode(userAlias);
        locks.setAliasSpecificLockMode(sqlAlias, effectiveLockMode);
        if (keyColumnNames != null) {
            keyColumnNames.put(sqlAlias, drivingPersister.getRootTableIdentifierColumnNames());
        }
    }
    // apply the collected locks and columns
    return dialect.applyLocksToSql(sql, locks, keyColumnNames);
}
Also used : LockOptions(org.hibernate.LockOptions) QueryNode(org.hibernate.hql.internal.ast.tree.QueryNode) Lockable(org.hibernate.persister.entity.Lockable) LockMode(org.hibernate.LockMode) HashMap(java.util.HashMap) Map(java.util.Map)

Example 23 with LockMode

use of org.hibernate.LockMode in project querydsl by querydsl.

the class HibernateInsertClause method execute.

@Override
public long execute() {
    JPQLSerializer serializer = new JPQLSerializer(templates, null);
    serializer.serializeForInsert(queryMixin.getMetadata(), inserts.isEmpty() ? columns : inserts.keySet(), values, subQuery, inserts);
    Query query = session.createQuery(serializer.toString());
    for (Map.Entry<Path<?>, LockMode> entry : lockModes.entrySet()) {
        query.setLockMode(entry.getKey().toString(), entry.getValue());
    }
    HibernateUtil.setConstants(query, serializer.getConstants(), queryMixin.getMetadata().getParams());
    return query.executeUpdate();
}
Also used : Path(com.querydsl.core.types.Path) EntityPath(com.querydsl.core.types.EntityPath) Query(org.hibernate.query.Query) JPQLSerializer(com.querydsl.jpa.JPQLSerializer) LockMode(org.hibernate.LockMode) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 24 with LockMode

use of org.hibernate.LockMode in project querydsl by querydsl.

the class HibernateDeleteClause method execute.

@Override
public long execute() {
    JPQLSerializer serializer = new JPQLSerializer(templates, null);
    serializer.serializeForDelete(queryMixin.getMetadata());
    Query query = session.createQuery(serializer.toString());
    for (Map.Entry<Path<?>, LockMode> entry : lockModes.entrySet()) {
        query.setLockMode(entry.getKey().toString(), entry.getValue());
    }
    HibernateUtil.setConstants(query, serializer.getConstants(), queryMixin.getMetadata().getParams());
    return query.executeUpdate();
}
Also used : Path(com.querydsl.core.types.Path) EntityPath(com.querydsl.core.types.EntityPath) Query(org.hibernate.query.Query) JPQLSerializer(com.querydsl.jpa.JPQLSerializer) LockMode(org.hibernate.LockMode) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with LockMode

use of org.hibernate.LockMode in project querydsl by querydsl.

the class HibernateUpdateClause method execute.

@Override
public long execute() {
    JPQLSerializer serializer = new JPQLSerializer(templates, null);
    serializer.serializeForUpdate(queryMixin.getMetadata(), updates);
    Query query = session.createQuery(serializer.toString());
    for (Map.Entry<Path<?>, LockMode> entry : lockModes.entrySet()) {
        query.setLockMode(entry.getKey().toString(), entry.getValue());
    }
    HibernateUtil.setConstants(query, serializer.getConstants(), queryMixin.getMetadata().getParams());
    return query.executeUpdate();
}
Also used : Path(com.querydsl.core.types.Path) EntityPath(com.querydsl.core.types.EntityPath) Query(org.hibernate.query.Query) JPQLSerializer(com.querydsl.jpa.JPQLSerializer) LockMode(org.hibernate.LockMode) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

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