Search in sources :

Example 46 with LockOptions

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

the class SessionImpl method find.

@Override
public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockModeType, Map<String, Object> properties) {
    checkOpen();
    LockOptions lockOptions = null;
    try {
        if (properties != null && !properties.isEmpty()) {
            getLoadQueryInfluencers().setFetchGraph((EntityGraph) properties.get(QueryHints.HINT_FETCHGRAPH));
            getLoadQueryInfluencers().setLoadGraph((EntityGraph) properties.get(QueryHints.HINT_LOADGRAPH));
        }
        final IdentifierLoadAccess<T> loadAccess = byId(entityClass);
        loadAccess.with(determineAppropriateLocalCacheMode(properties));
        if (lockModeType != null) {
            if (!LockModeType.NONE.equals(lockModeType)) {
                checkTransactionNeeded();
            }
            lockOptions = buildLockOptions(lockModeType, properties);
            loadAccess.with(lockOptions);
        }
        return loadAccess.load((Serializable) primaryKey);
    } catch (EntityNotFoundException ignored) {
        // which find() should not throw.  Find() should return null if the entity was not found.
        if (log.isDebugEnabled()) {
            String entityName = entityClass != null ? entityClass.getName() : null;
            String identifierValue = primaryKey != null ? primaryKey.toString() : null;
            log.ignoringEntityNotFound(entityName, identifierValue);
        }
        return null;
    } catch (ObjectDeletedException e) {
        //the spec is silent about people doing remove() find() on the same PC
        return null;
    } catch (ObjectNotFoundException e) {
        //should not happen on the entity itself with get
        throw new IllegalArgumentException(e.getMessage(), e);
    } catch (MappingException | TypeMismatchException | ClassCastException e) {
        throw exceptionConverter.convert(new IllegalArgumentException(e.getMessage(), e));
    } catch (RuntimeException e) {
        throw exceptionConverter.convert(e, lockOptions);
    } finally {
        getLoadQueryInfluencers().setFetchGraph(null);
        getLoadQueryInfluencers().setLoadGraph(null);
    }
}
Also used : LockOptions(org.hibernate.LockOptions) TypeMismatchException(org.hibernate.TypeMismatchException) EntityNotFoundException(javax.persistence.EntityNotFoundException) UnknownSqlResultSetMappingException(org.hibernate.procedure.UnknownSqlResultSetMappingException) MappingException(org.hibernate.MappingException) JPA_LOCK_TIMEOUT(org.hibernate.cfg.AvailableSettings.JPA_LOCK_TIMEOUT) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) ObjectDeletedException(org.hibernate.ObjectDeletedException)

Example 47 with LockOptions

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

the class SessionImpl method refresh.

@Override
public void refresh(Object entity, LockModeType lockModeType, Map<String, Object> properties) {
    checkOpen();
    final CacheMode previousCacheMode = getCacheMode();
    final CacheMode refreshCacheMode = determineAppropriateLocalCacheMode(properties);
    LockOptions lockOptions = null;
    try {
        setCacheMode(refreshCacheMode);
        if (!contains(entity)) {
            throw exceptionConverter.convert(new IllegalArgumentException("Entity not managed"));
        }
        if (lockModeType != null) {
            if (!LockModeType.NONE.equals(lockModeType)) {
                checkTransactionNeeded();
            }
            lockOptions = buildLockOptions(lockModeType, properties);
            refresh(entity, lockOptions);
        } else {
            refresh(entity);
        }
    } catch (MappingException e) {
        throw exceptionConverter.convert(new IllegalArgumentException(e.getMessage(), e));
    } catch (RuntimeException e) {
        throw exceptionConverter.convert(e, lockOptions);
    } finally {
        setCacheMode(previousCacheMode);
    }
}
Also used : LockOptions(org.hibernate.LockOptions) CacheMode(org.hibernate.CacheMode) UnknownSqlResultSetMappingException(org.hibernate.procedure.UnknownSqlResultSetMappingException) MappingException(org.hibernate.MappingException)

Example 48 with LockOptions

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

the class SessionImpl method lock.

@Override
public void lock(Object entity, LockModeType lockModeType, Map<String, Object> properties) {
    checkOpen();
    checkTransactionNeeded();
    if (!contains(entity)) {
        throw new IllegalArgumentException("entity not in the persistence context");
    }
    final LockOptions lockOptions = buildLockOptions(lockModeType, properties);
    try {
        buildLockRequest(lockOptions).lock(entity);
    } catch (RuntimeException e) {
        throw exceptionConverter.convert(e, lockOptions);
    }
}
Also used : LockOptions(org.hibernate.LockOptions)

Example 49 with LockOptions

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

the class CriteriaLoader method applyLocks.

@Override
protected String applyLocks(String sql, QueryParameters parameters, Dialect dialect, List<AfterLoadAction> afterLoadActions) throws QueryException {
    final LockOptions lockOptions = parameters.getLockOptions();
    if (lockOptions == null || (lockOptions.getLockMode() == LockMode.NONE && (lockOptions.getAliasLockCount() == 0 || (lockOptions.getAliasLockCount() == 1 && lockOptions.getAliasSpecificLockMode("this_") == LockMode.NONE)))) {
        return sql;
    }
    if ((parameters.getLockOptions().getFollowOnLocking() == null && dialect.useFollowOnLocking(parameters)) || (parameters.getLockOptions().getFollowOnLocking() != null && parameters.getLockOptions().getFollowOnLocking())) {
        final LockMode lockMode = determineFollowOnLockMode(lockOptions);
        if (lockMode != LockMode.UPGRADE_SKIPLOCKED) {
            // Dialect prefers to perform locking in a separate step
            LOG.usingFollowOnLocking();
            final LockOptions lockOptionsToUse = new LockOptions(lockMode);
            lockOptionsToUse.setTimeOut(lockOptions.getTimeOut());
            lockOptionsToUse.setScope(lockOptions.getScope());
            afterLoadActions.add(new AfterLoadAction() {

                @Override
                public void afterLoad(SharedSessionContractImplementor session, Object entity, Loadable persister) {
                    ((Session) session).buildLockRequest(lockOptionsToUse).lock(persister.getEntityName(), entity);
                }
            });
            parameters.setLockOptions(new LockOptions());
            return sql;
        }
    }
    final LockOptions locks = new LockOptions(lockOptions.getLockMode());
    locks.setScope(lockOptions.getScope());
    locks.setTimeOut(lockOptions.getTimeOut());
    final Map<String, String[]> keyColumnNames = dialect.forUpdateOfColumns() ? new HashMap() : null;
    final String[] drivingSqlAliases = getAliases();
    for (int i = 0; i < drivingSqlAliases.length; i++) {
        final LockMode lockMode = lockOptions.getAliasSpecificLockMode(drivingSqlAliases[i]);
        if (lockMode != null) {
            final Lockable drivingPersister = (Lockable) getEntityPersisters()[i];
            final String rootSqlAlias = drivingPersister.getRootTableAlias(drivingSqlAliases[i]);
            locks.setAliasSpecificLockMode(rootSqlAlias, lockMode);
            if (keyColumnNames != null) {
                keyColumnNames.put(rootSqlAlias, drivingPersister.getRootTableIdentifierColumnNames());
            }
        }
    }
    return dialect.applyLocksToSql(sql, locks, keyColumnNames);
}
Also used : OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) Loadable(org.hibernate.persister.entity.Loadable) LockOptions(org.hibernate.LockOptions) HashMap(java.util.HashMap) AfterLoadAction(org.hibernate.loader.spi.AfterLoadAction) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) LockMode(org.hibernate.LockMode) Lockable(org.hibernate.persister.entity.Lockable) Session(org.hibernate.Session)

Example 50 with LockOptions

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

the class OracleFollowOnLockingTest method testPessimisticLockWithMaxResultsAndOrderByThenFollowOnLocking.

@Test
public void testPessimisticLockWithMaxResultsAndOrderByThenFollowOnLocking() {
    final Session session = openSession();
    session.beginTransaction();
    sqlStatementInterceptor.getSqlQueries().clear();
    List<Product> products = session.createQuery("select p from Product p order by p.id", Product.class).setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE)).setMaxResults(10).getResultList();
    assertEquals(10, products.size());
    assertEquals(11, sqlStatementInterceptor.getSqlQueries().size());
    session.getTransaction().commit();
    session.close();
}
Also used : LockOptions(org.hibernate.LockOptions) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

LockOptions (org.hibernate.LockOptions)64 Test (org.junit.Test)43 Session (org.hibernate.Session)23 TestForIssue (org.hibernate.testing.TestForIssue)14 PersistenceException (javax.persistence.PersistenceException)5 SQLGrammarException (org.hibernate.exception.SQLGrammarException)5 LockMode (org.hibernate.LockMode)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 RowSelection (org.hibernate.engine.spi.RowSelection)3 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)3 AfterLoadAction (org.hibernate.loader.spi.AfterLoadAction)3 Loadable (org.hibernate.persister.entity.Loadable)3 RequiresDialect (org.hibernate.testing.RequiresDialect)3 Serializable (java.io.Serializable)2 CallableStatement (java.sql.CallableStatement)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2