use of javax.persistence.LockTimeoutException in project hibernate-orm by hibernate.
the class LockTest method testFindWithPessimisticWriteLockTimeoutException.
//5 minutes
@Test(timeout = 5 * 60 * 1000)
@TestForIssue(jiraKey = "HHH-7252")
@RequiresDialectFeature(value = DialectChecks.SupportsLockTimeouts.class, comment = "Test verifies proper exception throwing when a lock timeout is specified.", jiraKey = "HHH-7252")
public void testFindWithPessimisticWriteLockTimeoutException() {
Lock lock = new Lock();
lock.setName("name");
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(lock);
em.getTransaction().commit();
em.close();
EntityManager em2 = createIsolatedEntityManager();
em2.getTransaction().begin();
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(AvailableSettings.LOCK_TIMEOUT, 0L);
Lock lock2 = em2.find(Lock.class, lock.getId(), LockModeType.PESSIMISTIC_WRITE, properties);
assertEquals("lock mode should be PESSIMISTIC_WRITE ", LockModeType.PESSIMISTIC_WRITE, em2.getLockMode(lock2));
EntityManager em3 = createIsolatedEntityManager();
em3.getTransaction().begin();
try {
em3.find(Lock.class, lock.getId(), LockModeType.PESSIMISTIC_WRITE, properties);
fail("Exception should be thrown");
} catch (LockTimeoutException lte) {
// Proper exception thrown for dialect supporting lock timeouts when an immediate timeout is set.
} catch (PessimisticLockException pe) {
fail("Find with immediate timeout should have thrown LockTimeoutException.");
} catch (PersistenceException pe) {
log.info("EntityManager.find() for PESSIMISTIC_WRITE with timeout of 0 threw a PersistenceException.\n" + "This is likely a consequence of " + getDialect().getClass().getName() + " not properly mapping SQL errors into the correct HibernateException subtypes.\n" + "See HHH-7251 for an example of one such situation.", pe);
fail("EntityManager should be throwing LockTimeoutException.");
} finally {
if (em3.getTransaction().getRollbackOnly()) {
em3.getTransaction().rollback();
} else {
em3.getTransaction().commit();
}
em3.close();
}
em2.getTransaction().commit();
em2.getTransaction().begin();
em2.remove(lock2);
em2.getTransaction().commit();
em2.close();
}
use of javax.persistence.LockTimeoutException in project hibernate-orm by hibernate.
the class LockTest method testContendedPessimisticReadLockTimeout.
@Test
@RequiresDialect(Oracle10gDialect.class)
@RequiresDialectFeature(DialectChecks.SupportsLockTimeouts.class)
public void testContendedPessimisticReadLockTimeout() throws Exception {
EntityManager em = getOrCreateEntityManager();
final EntityManager em2 = createIsolatedEntityManager();
Lock lock = new Lock();
Thread t = null;
FutureTask<Boolean> bgTask = null;
final CountDownLatch latch = new CountDownLatch(1);
try {
lock.setName("testContendedPessimisticReadLockTimeout");
em.getTransaction().begin();
em.persist(lock);
em.getTransaction().commit();
em.clear();
em.getTransaction().begin();
lock = em.getReference(Lock.class, lock.getId());
em.lock(lock, LockModeType.PESSIMISTIC_WRITE);
final Integer id = lock.getId();
// force entity to be read
lock.getName();
log.info("testContendedPessimisticReadLockTimeout: got write lock");
bgTask = new FutureTask<Boolean>(new Callable<Boolean>() {
public Boolean call() {
try {
// true (success) if LockTimeoutException occurred
boolean timedOut = false;
em2.getTransaction().begin();
log.info("testContendedPessimisticReadLockTimeout: (BG) about to read write-locked entity");
// we should block on the following read
Lock lock2 = em2.getReference(Lock.class, id);
// force entity to be read
lock2.getName();
log.info("testContendedPessimisticReadLockTimeout: (BG) read write-locked entity");
Map<String, Object> props = new HashMap<String, Object>();
// timeout is in milliseconds
props.put(AvailableSettings.LOCK_TIMEOUT, 1000);
try {
em2.lock(lock2, LockModeType.PESSIMISTIC_READ, props);
} catch (LockTimeoutException e) {
// success
log.info("testContendedPessimisticReadLockTimeout: (BG) got expected timeout exception");
timedOut = true;
em2.getTransaction().rollback();
return timedOut;
} catch (Throwable e) {
log.info("Expected LockTimeoutException but got unexpected exception", e);
throw new RuntimeException("Expected LockTimeoutException but got unexpected exception", e);
}
em2.getTransaction().commit();
return timedOut;
} finally {
// signal that we finished
latch.countDown();
}
}
});
t = new Thread(bgTask);
t.setDaemon(true);
t.setName("Lock timeout Test (bg)");
t.start();
// should return quickly on success
boolean latchSet = latch.await(10, TimeUnit.SECONDS);
assertTrue("background test thread finished (lock timeout is broken)", latchSet);
assertTrue("background test thread timed out on lock attempt", bgTask.get());
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if (t != null) {
// wait for background thread to finish beforeQuery deleting entity
t.join();
}
em.getTransaction().begin();
lock = em.getReference(Lock.class, lock.getId());
em.remove(lock);
em.getTransaction().commit();
em.close();
em2.close();
}
}
use of javax.persistence.LockTimeoutException in project hibernate-orm by hibernate.
the class LockTest method testContendedPessimisticWriteLockTimeout.
@Test
@RequiresDialect(Oracle10gDialect.class)
@RequiresDialectFeature(DialectChecks.SupportsLockTimeouts.class)
public void testContendedPessimisticWriteLockTimeout() throws Exception {
EntityManager em = getOrCreateEntityManager();
final EntityManager em2 = createIsolatedEntityManager();
Lock lock = new Lock();
Thread t = null;
FutureTask<Boolean> bgTask;
final CountDownLatch latch = new CountDownLatch(1);
try {
lock.setName("testContendedPessimisticWriteLockTimeout");
em.getTransaction().begin();
em.persist(lock);
em.getTransaction().commit();
em.clear();
em.getTransaction().begin();
lock = em.getReference(Lock.class, lock.getId());
em.lock(lock, LockModeType.PESSIMISTIC_WRITE);
final Integer id = lock.getId();
// force entity to be read
lock.getName();
log.info("testContendedPessimisticWriteLockTimeout: got write lock");
bgTask = new FutureTask<Boolean>(new Callable<Boolean>() {
public Boolean call() {
try {
// true (success) if LockTimeoutException occurred
boolean timedOut = false;
em2.getTransaction().begin();
log.info("testContendedPessimisticWriteLockTimeout: (BG) about to read write-locked entity");
// we should block on the following read
Lock lock2 = em2.getReference(Lock.class, id);
// force entity to be read
lock2.getName();
log.info("testContendedPessimisticWriteLockTimeout: (BG) read write-locked entity");
Map<String, Object> props = new HashMap<String, Object>();
// timeout is in milliseconds
props.put(AvailableSettings.LOCK_TIMEOUT, 1000);
try {
em2.lock(lock2, LockModeType.PESSIMISTIC_WRITE, props);
} catch (LockTimeoutException e) {
// success
log.info("testContendedPessimisticWriteLockTimeout: (BG) got expected timeout exception");
timedOut = true;
} catch (Throwable e) {
log.info("Expected LockTimeoutException but got unexpected exception", e);
}
em2.getTransaction().commit();
return timedOut;
} finally {
// signal that we finished
latch.countDown();
}
}
});
t = new Thread(bgTask);
t.setDaemon(true);
t.setName("Lock timeout Test (bg)");
t.start();
// should return quickly on success
boolean latchSet = latch.await(10, TimeUnit.SECONDS);
assertTrue("background test thread finished (lock timeout is broken)", latchSet);
assertTrue("background test thread timed out on lock attempt", bgTask.get());
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if (t != null) {
// wait for background thread to finish beforeQuery deleting entity
t.join();
}
em.getTransaction().begin();
lock = em.getReference(Lock.class, lock.getId());
em.remove(lock);
em.getTransaction().commit();
em.close();
em2.close();
}
}
use of javax.persistence.LockTimeoutException in project hibernate-orm by hibernate.
the class LockTest method testLockTimeoutEMProps.
@Test
@RequiresDialect(Oracle10gDialect.class)
@RequiresDialectFeature(DialectChecks.SupportsLockTimeouts.class)
public void testLockTimeoutEMProps() throws Exception {
EntityManager em = getOrCreateEntityManager();
Map<String, Object> TimeoutProps = new HashMap<String, Object>();
// 1 second timeout
TimeoutProps.put(AvailableSettings.LOCK_TIMEOUT, 1000);
final EntityManager em2 = createIsolatedEntityManager(TimeoutProps);
Lock lock = new Lock();
Thread t = null;
FutureTask<Boolean> bgTask;
final CountDownLatch latch = new CountDownLatch(1);
try {
lock.setName("testLockTimeoutEMProps");
em.getTransaction().begin();
em.persist(lock);
em.getTransaction().commit();
em.clear();
em.getTransaction().begin();
lock = em.getReference(Lock.class, lock.getId());
em.lock(lock, LockModeType.PESSIMISTIC_WRITE);
final Integer id = lock.getId();
// force entity to be read
lock.getName();
log.info("testLockTimeoutEMProps: got write lock");
bgTask = new FutureTask<Boolean>(new Callable<Boolean>() {
public Boolean call() {
try {
// true (success) if LockTimeoutException occurred
boolean timedOut = false;
em2.getTransaction().begin();
log.info("testLockTimeoutEMProps: (BG) about to read write-locked entity");
// we should block on the following read
Lock lock2 = em2.getReference(Lock.class, id);
// force entity to be read
lock2.getName();
log.info("testLockTimeoutEMProps: (BG) read write-locked entity");
// em2 already has AvailableSettings.LOCK_TIMEOUT of 1 second applied
try {
em2.lock(lock2, LockModeType.PESSIMISTIC_WRITE);
} catch (LockTimeoutException e) {
// success
log.info("testLockTimeoutEMProps: (BG) got expected timeout exception");
timedOut = true;
} catch (Throwable e) {
log.info("Expected LockTimeoutException but got unexpected exception", e);
}
em2.getTransaction().commit();
return timedOut;
} finally {
// signal that we finished
latch.countDown();
}
}
});
t = new Thread(bgTask);
t.setDaemon(true);
t.setName("Lock timeout Test (bg)");
t.start();
// should return quickly on success
boolean latchSet = latch.await(10, TimeUnit.SECONDS);
assertTrue("background test thread finished (lock timeout is broken)", latchSet);
assertTrue("background test thread timed out on lock attempt", bgTask.get());
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if (t != null) {
// wait for background thread to finish beforeQuery deleting entity
t.join();
}
em.getTransaction().begin();
lock = em.getReference(Lock.class, lock.getId());
em.remove(lock);
em.getTransaction().commit();
em.close();
em2.close();
}
}
use of javax.persistence.LockTimeoutException in project hibernate-orm by hibernate.
the class LockTest method testContendedPessimisticWriteLockNoWait.
@Test
@RequiresDialect({ Oracle10gDialect.class, PostgreSQL81Dialect.class })
@RequiresDialectFeature(DialectChecks.SupportsLockTimeouts.class)
public void testContendedPessimisticWriteLockNoWait() throws Exception {
EntityManager em = getOrCreateEntityManager();
final EntityManager em2 = createIsolatedEntityManager();
Lock lock = new Lock();
Thread t = null;
FutureTask<Boolean> bgTask;
final CountDownLatch latch = new CountDownLatch(1);
try {
lock.setName("testContendedPessimisticWriteLockNoWait");
em.getTransaction().begin();
em.persist(lock);
em.getTransaction().commit();
em.clear();
em.getTransaction().begin();
lock = em.getReference(Lock.class, lock.getId());
em.lock(lock, LockModeType.PESSIMISTIC_WRITE);
final Integer id = lock.getId();
// force entity to be read
lock.getName();
log.info("testContendedPessimisticWriteLockNoWait: got write lock");
bgTask = new FutureTask<Boolean>(new Callable<Boolean>() {
public Boolean call() {
try {
// true (success) if LockTimeoutException occurred
boolean timedOut = false;
em2.getTransaction().begin();
log.info("testContendedPessimisticWriteLockNoWait: (BG) about to read write-locked entity");
// we should block on the following read
Lock lock2 = em2.getReference(Lock.class, id);
// force entity to be read
lock2.getName();
log.info("testContendedPessimisticWriteLockNoWait: (BG) read write-locked entity");
Map<String, Object> props = new HashMap<String, Object>();
// timeout of zero means no wait (for lock)
props.put(AvailableSettings.LOCK_TIMEOUT, 0);
try {
em2.lock(lock2, LockModeType.PESSIMISTIC_WRITE, props);
} catch (LockTimeoutException e) {
// success
log.info("testContendedPessimisticWriteLockNoWait: (BG) got expected timeout exception");
timedOut = true;
} catch (Throwable e) {
log.info("Expected LockTimeoutException but got unexpected exception", e);
}
em2.getTransaction().commit();
return timedOut;
} finally {
// signal that we finished
latch.countDown();
}
}
});
t = new Thread(bgTask);
t.setDaemon(true);
t.setName("Lock timeout Test (bg)");
t.start();
// should return quickly on success
boolean latchSet = latch.await(10, TimeUnit.SECONDS);
assertTrue("background test thread finished (lock timeout is broken)", latchSet);
assertTrue("background test thread timed out on lock attempt", bgTask.get());
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if (t != null) {
// wait for background thread to finish beforeQuery deleting entity
t.join();
}
em.getTransaction().begin();
lock = em.getReference(Lock.class, lock.getId());
em.remove(lock);
em.getTransaction().commit();
em.close();
em2.close();
}
}
Aggregations