Search in sources :

Example 1 with LockTimeoutException

use of org.hibernate.exception.LockTimeoutException 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)

Aggregations

LockOptions (org.hibernate.LockOptions)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1 LockTimeoutException (org.hibernate.exception.LockTimeoutException)1 TestForIssue (org.hibernate.testing.TestForIssue)1 Test (org.junit.Test)1