Search in sources :

Example 26 with LockOptions

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

the class PostgreSQLLockTimeoutTest method testLockTimeoutNoAliasSkipLocked.

@Test
public void testLockTimeoutNoAliasSkipLocked() {
    assertEquals(" for share skip locked", dialect.getForUpdateString(new LockOptions(LockMode.PESSIMISTIC_READ).setTimeOut(LockOptions.SKIP_LOCKED)));
    assertEquals(" for update skip locked", dialect.getForUpdateString(new LockOptions(LockMode.PESSIMISTIC_WRITE).setTimeOut(LockOptions.SKIP_LOCKED)));
}
Also used : LockOptions(org.hibernate.LockOptions) Test(org.junit.Test)

Example 27 with LockOptions

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

the class PostgreSQLLockTimeoutTest method testLockTimeoutAliasNoTimeout.

@Test
public void testLockTimeoutAliasNoTimeout() {
    String alias = "a";
    assertEquals(" for share of a", dialect.getForUpdateString(alias, new LockOptions(LockMode.PESSIMISTIC_READ).setAliasSpecificLockMode(alias, LockMode.PESSIMISTIC_READ)));
    assertEquals(" for update of a", dialect.getForUpdateString(alias, new LockOptions(LockMode.PESSIMISTIC_WRITE).setAliasSpecificLockMode(alias, LockMode.PESSIMISTIC_WRITE)));
}
Also used : LockOptions(org.hibernate.LockOptions) Test(org.junit.Test)

Example 28 with LockOptions

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

the class LockModeTest method testQueryUsingLockOptions.

@Test
public void testQueryUsingLockOptions() {
    // todo : need an association here to make sure the alias-specific lock modes are applied correctly
    doInHibernate(this::sessionFactory, session -> {
        session.createQuery("from A a").setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE)).uniqueResult();
        session.createQuery("from A a").setLockOptions(new LockOptions().setAliasSpecificLockMode("a", LockMode.PESSIMISTIC_WRITE)).uniqueResult();
    });
}
Also used : LockOptions(org.hibernate.LockOptions) Test(org.junit.Test)

Example 29 with LockOptions

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

the class PessimisticWriteLockTimeoutTest method testNoWait.

@Test
@RequiresDialect({ Oracle8iDialect.class, PostgreSQL81Dialect.class, SQLServer2005Dialect.class })
public void testNoWait() throws NoSuchFieldException, IllegalAccessException {
    Session session = sessionFactory().openSession();
    session.beginTransaction();
    try {
        session.createQuery("select a from A a", A.class).unwrap(org.hibernate.query.Query.class).setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE).setTimeOut(LockOptions.NO_WAIT)).list();
        String lockingQuery = sqlStatementInterceptor.getSqlQueries().getLast();
        assertTrue(lockingQuery.toLowerCase().contains("nowait"));
    } finally {
        session.getTransaction().commit();
        session.close();
    }
}
Also used : LockOptions(org.hibernate.LockOptions) Session(org.hibernate.Session) Test(org.junit.Test) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 30 with LockOptions

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

the class PessimisticWriteLockTimeoutTest method testSkipLocked.

@Test
@RequiresDialect({ Oracle8iDialect.class, PostgreSQL95Dialect.class })
public void testSkipLocked() throws NoSuchFieldException, IllegalAccessException {
    Session session = sessionFactory().openSession();
    session.beginTransaction();
    try {
        session.createQuery("select a from A a", A.class).unwrap(org.hibernate.query.Query.class).setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE).setTimeOut(LockOptions.SKIP_LOCKED)).list();
        String lockingQuery = sqlStatementInterceptor.getSqlQueries().getLast();
        assertTrue(lockingQuery.toLowerCase().contains("skip locked"));
    } finally {
        session.getTransaction().commit();
        session.close();
    }
}
Also used : LockOptions(org.hibernate.LockOptions) Session(org.hibernate.Session) Test(org.junit.Test) RequiresDialect(org.hibernate.testing.RequiresDialect)

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