Search in sources :

Example 51 with LockOptions

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

the class OracleFollowOnLockingTest method testPessimisticLockWithFirstResultsWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking.

@Test
public void testPessimisticLockWithFirstResultsWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking() {
    final Session session = openSession();
    session.beginTransaction();
    sqlStatementInterceptor.getSqlQueries().clear();
    List<Product> products = session.createQuery("select p from Product p", Product.class).setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE).setFollowOnLocking(true)).setFirstResult(40).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)

Example 52 with LockOptions

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

the class OracleFollowOnLockingTest method testPessimisticLockWithGroupByWhileExplicitlyDisablingFollowOnLockingThenFails.

@Test
public void testPessimisticLockWithGroupByWhileExplicitlyDisablingFollowOnLockingThenFails() {
    final Session session = openSession();
    session.beginTransaction();
    sqlStatementInterceptor.getSqlQueries().clear();
    try {
        List<Object[]> products = session.createQuery("select count(p), p " + "from Product p " + "group by p.id, p.name ").setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE).setFollowOnLocking(false)).getResultList();
        fail("Should throw exception since Oracle does not support GROUP BY if follow on locking is disabled");
    } catch (PersistenceException expected) {
        assertEquals(SQLGrammarException.class, expected.getCause().getClass());
    }
}
Also used : LockOptions(org.hibernate.LockOptions) SQLGrammarException(org.hibernate.exception.SQLGrammarException) PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 53 with LockOptions

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

the class SQLServerDialectTest method testLockNowaitSqlServer.

@Test
@TestForIssue(jiraKey = "HHH-3961")
public void testLockNowaitSqlServer() throws Exception {
    Session s = openSession();
    s.beginTransaction();
    final Product2 kit = new Product2();
    kit.id = 4000;
    kit.description = "m";
    s.persist(kit);
    s.getTransaction().commit();
    final Transaction tx = s.beginTransaction();
    Session s2 = openSession();
    s2.beginTransaction();
    Product2 kit2 = s2.byId(Product2.class).load(kit.id);
    kit.description = "change!";
    // creates write lock on kit until we end the transaction
    s.flush();
    Thread thread = new Thread(() -> {
        sleep(TimeUnit.SECONDS.toMillis(1));
        tx.commit();
    });
    LockOptions opt = new LockOptions(LockMode.UPGRADE_NOWAIT);
    // seems useless
    opt.setTimeOut(0);
    long start = System.currentTimeMillis();
    thread.start();
    try {
        s2.buildLockRequest(opt).lock(kit2);
    } catch (LockTimeoutException e) {
    // OK
    }
    long end = System.currentTimeMillis();
    thread.join();
    long differenceInMillis = end - start;
    assertTrue("Lock NoWait blocked for " + differenceInMillis + " ms, this is definitely to much for Nowait", differenceInMillis < 2000);
    s2.getTransaction().rollback();
    s.getTransaction().begin();
    s.delete(kit);
    s.getTransaction().commit();
}
Also used : LockOptions(org.hibernate.LockOptions) Transaction(org.hibernate.Transaction) LockTimeoutException(org.hibernate.exception.LockTimeoutException) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 54 with LockOptions

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

the class AbstractLockHintTest method lockOptions.

protected LockOptions lockOptions(String aliasToLock) {
    LockOptions lockOptions = new LockOptions(LockMode.UPGRADE);
    lockOptions.setAliasSpecificLockMode(aliasToLock, LockMode.UPGRADE);
    return lockOptions;
}
Also used : LockOptions(org.hibernate.LockOptions)

Example 55 with LockOptions

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

the class HANALockTimeoutTest method testLockTimeoutAliasNoTimeout.

@Test
public void testLockTimeoutAliasNoTimeout() {
    String alias = "a";
    assertEquals(" for update 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)

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