Search in sources :

Example 1 with LockOptions

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

the class ExplicitLockingTest method testFollowOnLocking.

@Test
@RequiresDialect(Oracle8iDialect.class)
public void testFollowOnLocking() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("testBuildLockRequest");
        Person person1 = new Person("John Doe");
        Person person2 = new Person("Mrs. John Doe");
        entityManager.persist(person1);
        entityManager.persist(person2);
        entityManager.flush();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        List<Person> persons = entityManager.createQuery("select DISTINCT p from Person p", Person.class).setLockMode(LockModeType.PESSIMISTIC_WRITE).getResultList();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        List<Person> persons = entityManager.createQuery("select DISTINCT p from Person p", Person.class).getResultList();
        entityManager.createQuery("select p.id from Person p where p in :persons").setLockMode(LockModeType.PESSIMISTIC_WRITE).setParameter("persons", persons).getResultList();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        List<Person> persons = entityManager.createQuery("select p from Person p", Person.class).setMaxResults(10).unwrap(Query.class).setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE).setFollowOnLocking(false)).getResultList();
    });
}
Also used : LockOptions(org.hibernate.LockOptions) Test(org.junit.Test) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 2 with LockOptions

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

the class PessimisticReadSelectLockingStrategy method generateLockString.

protected String generateLockString(int lockTimeout) {
    final SessionFactoryImplementor factory = getLockable().getFactory();
    final LockOptions lockOptions = new LockOptions(getLockMode());
    lockOptions.setTimeOut(lockTimeout);
    final SimpleSelect select = new SimpleSelect(factory.getDialect()).setLockOptions(lockOptions).setTableName(getLockable().getRootTableName()).addColumn(getLockable().getRootTableIdentifierColumnNames()[0]).addCondition(getLockable().getRootTableIdentifierColumnNames(), "=?");
    if (getLockable().isVersioned()) {
        select.addCondition(getLockable().getVersionColumnName(), "=?");
    }
    if (factory.getSessionFactoryOptions().isCommentsEnabled()) {
        select.setComment(getLockMode() + " lock " + getLockable().getEntityName());
    }
    return select.toStatementString();
}
Also used : LockOptions(org.hibernate.LockOptions) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SimpleSelect(org.hibernate.sql.SimpleSelect)

Example 3 with LockOptions

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

the class PessimisticWriteSelectLockingStrategy method generateLockString.

protected String generateLockString(int lockTimeout) {
    final SessionFactoryImplementor factory = getLockable().getFactory();
    final LockOptions lockOptions = new LockOptions(getLockMode());
    lockOptions.setTimeOut(lockTimeout);
    final SimpleSelect select = new SimpleSelect(factory.getDialect()).setLockOptions(lockOptions).setTableName(getLockable().getRootTableName()).addColumn(getLockable().getRootTableIdentifierColumnNames()[0]).addCondition(getLockable().getRootTableIdentifierColumnNames(), "=?");
    if (getLockable().isVersioned()) {
        select.addCondition(getLockable().getVersionColumnName(), "=?");
    }
    if (factory.getSessionFactoryOptions().isCommentsEnabled()) {
        select.setComment(getLockMode() + " lock " + getLockable().getEntityName());
    }
    return select.toStatementString();
}
Also used : LockOptions(org.hibernate.LockOptions) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SimpleSelect(org.hibernate.sql.SimpleSelect)

Example 4 with LockOptions

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

the class QueryTranslatorImpl 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)
    final LockOptions lockOptions = parameters.getLockOptions();
    final String result;
    if (lockOptions == null || (lockOptions.getLockMode() == LockMode.NONE && lockOptions.getAliasLockCount() == 0)) {
        return sql;
    } else {
        LockOptions locks = new LockOptions();
        locks.setLockMode(lockOptions.getLockMode());
        locks.setTimeOut(lockOptions.getTimeOut());
        locks.setScope(lockOptions.getScope());
        Iterator iter = lockOptions.getAliasLockIterator();
        while (iter.hasNext()) {
            Map.Entry me = (Map.Entry) iter.next();
            locks.setAliasSpecificLockMode(getAliasName((String) me.getKey()), (LockMode) me.getValue());
        }
        Map keyColumnNames = null;
        if (dialect.forUpdateOfColumns()) {
            keyColumnNames = new HashMap();
            for (int i = 0; i < names.length; i++) {
                keyColumnNames.put(names[i], persisters[i].getIdentifierColumnNames());
            }
        }
        result = dialect.applyLocksToSql(sql, locks, keyColumnNames);
    }
    logQuery(queryString, result);
    return result;
}
Also used : LockOptions(org.hibernate.LockOptions) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Iterator(java.util.Iterator) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with LockOptions

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

the class SessionImpl method getLockRequest.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// HibernateEntityManagerImplementor impl
@Override
public LockOptions getLockRequest(LockModeType lockModeType, Map<String, Object> properties) {
    LockOptions lockOptions = new LockOptions();
    LockOptions.copy(this.lockOptions, lockOptions);
    lockOptions.setLockMode(LockModeTypeHelper.getLockMode(lockModeType));
    if (properties != null) {
        setLockOptions(properties, lockOptions);
    }
    return 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