Search in sources :

Example 61 with LockOptions

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

the class TableGenerator method buildSelectQuery.

@SuppressWarnings("unchecked")
protected String buildSelectQuery(Dialect dialect) {
    final String alias = "tbl";
    final String query = "select " + StringHelper.qualify(alias, valueColumnName) + " from " + renderedTableName + ' ' + alias + " where " + StringHelper.qualify(alias, segmentColumnName) + "=?";
    final LockOptions lockOptions = new LockOptions(LockMode.PESSIMISTIC_WRITE);
    lockOptions.setAliasSpecificLockMode(alias, LockMode.PESSIMISTIC_WRITE);
    final Map updateTargetColumnsMap = Collections.singletonMap(alias, new String[] { valueColumnName });
    return dialect.applyLocksToSql(query, lockOptions, updateTargetColumnsMap);
}
Also used : LockOptions(org.hibernate.LockOptions) Map(java.util.Map)

Example 62 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 63 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 64 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)

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