use of org.hibernate.testing.RequiresDialect in project hibernate-orm by hibernate.
the class ExpressionsTest method testEmptyInList.
@Test
@TestForIssue(jiraKey = "HHH-6876")
@RequiresDialect(H2Dialect.class)
public void testEmptyInList() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
Root<Product> from = criteria.from(Product.class);
// empty IN list
criteria.where(from.get(Product_.partNumber).in());
List<Product> result = em.createQuery(criteria).getResultList();
assertEquals(0, result.size());
em.getTransaction().commit();
em.close();
}
use of org.hibernate.testing.RequiresDialect 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 org.hibernate.testing.RequiresDialect 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 org.hibernate.testing.RequiresDialect in project hibernate-orm by hibernate.
the class LockTest method testQueryTimeoutEMProps.
@Test
@RequiresDialect(Oracle10gDialect.class)
@RequiresDialectFeature(DialectChecks.SupportsLockTimeouts.class)
@FailureExpected(jiraKey = "HHH-8001")
public void testQueryTimeoutEMProps() throws Exception {
EntityManager em = getOrCreateEntityManager();
Map<String, Object> queryTimeoutProps = new HashMap<String, Object>();
// 1 sec timeout (should round up)
queryTimeoutProps.put(QueryHints.SPEC_HINT_TIMEOUT, 500);
final EntityManager em2 = createIsolatedEntityManager(queryTimeoutProps);
Lock lock = new Lock();
Thread t = null;
FutureTask<Boolean> bgTask;
final CountDownLatch latch = new CountDownLatch(1);
try {
lock.setName("testQueryTimeout");
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("testQueryTimeout: 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("testQueryTimeout: (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("testQueryTimeout: (BG) read write-locked entity");
try {
// we should block on the following read
Query query = em2.createQuery("select L from Lock_ L where L.id < 10000 ");
query.setLockMode(LockModeType.PESSIMISTIC_READ);
List<Lock> resultList = query.getResultList();
// force entity to be read
String name = resultList.get(0).getName();
log.info("testQueryTimeout: name read =" + name);
} catch (QueryTimeoutException e) {
// success
log.info("testQueryTimeout: (BG) got expected timeout exception");
timedOut = true;
} catch (Throwable e) {
log.info("testQueryTimeout: 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("testQueryTimeout (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 org.hibernate.testing.RequiresDialect in project hibernate-orm by hibernate.
the class QueryLockingTest method testCriteriaWithPessimisticLock.
@Test
@TestForIssue(jiraKey = "HHH-11376")
@RequiresDialect(SQLServerDialect.class)
public void testCriteriaWithPessimisticLock() {
TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
Root<Person> personRoot = criteria.from(Person.class);
ParameterExpression<Long> personIdParameter = builder.parameter(Long.class);
personRoot.fetch("parent", JoinType.LEFT);
criteria.select(personRoot).where(builder.equal(personRoot.get("id"), personIdParameter));
final List<Person> resultList = entityManager.createQuery(criteria).setParameter(personIdParameter, 1L).setLockMode(LockModeType.PESSIMISTIC_WRITE).getResultList();
resultList.isEmpty();
});
}
Aggregations