Search in sources :

Example 11 with RequiresDialect

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();
}
Also used : EntityManager(javax.persistence.EntityManager) Product(org.hibernate.jpa.test.metamodel.Product) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest) TestForIssue(org.hibernate.testing.TestForIssue) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 12 with RequiresDialect

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();
    }
}
Also used : HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) EntityManager(javax.persistence.EntityManager) LockTimeoutException(javax.persistence.LockTimeoutException) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 13 with RequiresDialect

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();
    }
}
Also used : HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) EntityManager(javax.persistence.EntityManager) LockTimeoutException(javax.persistence.LockTimeoutException) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 14 with RequiresDialect

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();
    }
}
Also used : Query(javax.persistence.Query) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) QueryTimeoutException(javax.persistence.QueryTimeoutException) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) FailureExpected(org.hibernate.testing.FailureExpected) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 15 with RequiresDialect

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();
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue) RequiresDialect(org.hibernate.testing.RequiresDialect)

Aggregations

RequiresDialect (org.hibernate.testing.RequiresDialect)36 Test (org.junit.Test)34 Session (org.hibernate.Session)18 TestForIssue (org.hibernate.testing.TestForIssue)13 EntityManager (javax.persistence.EntityManager)9 RequiresDialectFeature (org.hibernate.testing.RequiresDialectFeature)7 Callable (java.util.concurrent.Callable)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Transaction (org.hibernate.Transaction)6 HashMap (java.util.HashMap)5 Phone (org.hibernate.userguide.model.Phone)5 LockTimeoutException (javax.persistence.LockTimeoutException)4 List (java.util.List)3 LockOptions (org.hibernate.LockOptions)3 FailureExpected (org.hibernate.testing.FailureExpected)3 Call (org.hibernate.userguide.model.Call)3 Query (javax.persistence.Query)2 QueryTimeoutException (javax.persistence.QueryTimeoutException)2 MetadataSources (org.hibernate.boot.MetadataSources)2 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)2