Search in sources :

Example 1 with QueryTimeoutException

use of javax.persistence.QueryTimeoutException in project hibernate-orm by hibernate.

the class LockTest method testQueryTimeoutEMProps.

@Test
@RequiresDialect(Oracle10gDialect.class)
@RequiresDialectFeature(DialectChecks.SupportsLockTimeouts.class)
public void testQueryTimeoutEMProps() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final Map<String, Object> timeoutProps = new HashMap<String, Object>();
    // 1 sec timeout (should round up)
    timeoutProps.put(QueryHints.SPEC_HINT_TIMEOUT, 500);
    final Lock lock = new Lock();
    FutureTask<Boolean> bgTask = new FutureTask<>(() -> {
        try {
            // true (success) if LockTimeoutException occurred
            AtomicBoolean timedOut = new AtomicBoolean();
            doInJPA(this::entityManagerFactory, _entityManager -> {
                log.info("testQueryTimeout: (BG) about to read write-locked entity");
                // we should block on the following read
                Lock lock2 = _entityManager.getReference(Lock.class, lock.getId());
                // 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 = _entityManager.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.set(true);
                } catch (Throwable e) {
                    log.info("Expected LockTimeoutException but got unexpected exception", e);
                }
            }, timeoutProps);
            return timedOut.get();
        } finally {
            // signal that we finished
            latch.countDown();
        }
    });
    Thread t = new Thread(bgTask);
    t.setDaemon(true);
    t.setName("Lock timeout Test (bg)");
    try {
        lock.setName("testQueryTimeout");
        doInJPA(this::entityManagerFactory, em -> {
            em.persist(lock);
        });
        doInJPA(this::entityManagerFactory, em -> {
            Lock _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");
            try {
                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());
            } catch (InterruptedException e) {
                Thread.interrupted();
            } catch (ExecutionException e) {
                fail(e.getMessage());
            }
        });
    } finally {
        // wait for background thread to finish before deleting entity
        t.join();
        doInJPA(this::entityManagerFactory, em -> {
            Lock _lock = em.getReference(Lock.class, lock.getId());
            em.remove(_lock);
        });
    }
}
Also used : Query(javax.persistence.Query) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QueryTimeoutException(javax.persistence.QueryTimeoutException) FutureTask(java.util.concurrent.FutureTask) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 2 with QueryTimeoutException

use of javax.persistence.QueryTimeoutException in project oozie by apache.

the class TestPersistenceExceptionSubclassFilterRetryPredicate method testNestedFilteredJPAExceptions.

@Test
public void testNestedFilteredJPAExceptions() {
    assertFalse(predicate.apply(wrapCause(new EntityExistsException())));
    assertFalse(predicate.apply(wrapCause(new EntityNotFoundException())));
    assertFalse(predicate.apply(wrapCause(new LockTimeoutException())));
    assertFalse(predicate.apply(wrapCause(new NoResultException())));
    assertFalse(predicate.apply(wrapCause(new NonUniqueResultException())));
    assertFalse(predicate.apply(wrapCause(new OptimisticLockException())));
    assertFalse(predicate.apply(wrapCause(new PessimisticLockException())));
    assertFalse(predicate.apply(wrapCause(new QueryTimeoutException())));
    assertFalse(predicate.apply(wrapCause(new TransactionRequiredException())));
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) QueryTimeoutException(javax.persistence.QueryTimeoutException) TransactionRequiredException(javax.persistence.TransactionRequiredException) OptimisticLockException(javax.persistence.OptimisticLockException) EntityNotFoundException(javax.persistence.EntityNotFoundException) NoResultException(javax.persistence.NoResultException) EntityExistsException(javax.persistence.EntityExistsException) LockTimeoutException(javax.persistence.LockTimeoutException) PessimisticLockException(javax.persistence.PessimisticLockException) Test(org.junit.Test)

Example 3 with QueryTimeoutException

use of javax.persistence.QueryTimeoutException in project hibernate-orm by hibernate.

the class LockTest method testQueryTimeout.

@Test
@RequiresDialect(Oracle10gDialect.class)
@RequiresDialectFeature(DialectChecks.SupportsLockTimeouts.class)
public void testQueryTimeout() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final Lock lock = new Lock();
    FutureTask<Boolean> bgTask = new FutureTask<>(() -> {
        try {
            // true (success) if LockTimeoutException occurred
            AtomicBoolean timedOut = new AtomicBoolean();
            doInJPA(this::entityManagerFactory, _entityManager -> {
                log.info("testQueryTimeout: (BG) about to read write-locked entity");
                // we should block on the following read
                Lock lock2 = _entityManager.getReference(Lock.class, lock.getId());
                // 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 = _entityManager.createQuery("select L from Lock_ L where L.id < 10000 ");
                    query.setLockMode(LockModeType.PESSIMISTIC_READ);
                    // 1 sec timeout
                    query.setHint(QueryHints.SPEC_HINT_TIMEOUT, 500);
                    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.set(true);
                } catch (Throwable e) {
                    log.info("Expected LockTimeoutException but got unexpected exception", e);
                }
            });
            return timedOut.get();
        } finally {
            // signal that we finished
            latch.countDown();
        }
    });
    Thread t = new Thread(bgTask);
    t.setDaemon(true);
    t.setName("Lock timeout Test (bg)");
    try {
        lock.setName("testQueryTimeout");
        doInJPA(this::entityManagerFactory, em -> {
            em.persist(lock);
        });
        doInJPA(this::entityManagerFactory, em -> {
            Lock _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");
            try {
                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());
            } catch (InterruptedException e) {
                Thread.interrupted();
            } catch (ExecutionException e) {
                fail(e.getMessage());
            }
        });
    } finally {
        // wait for background thread to finish before deleting entity
        t.join();
        doInJPA(this::entityManagerFactory, em -> {
            Lock _lock = em.getReference(Lock.class, lock.getId());
            em.remove(_lock);
        });
    }
}
Also used : Query(javax.persistence.Query) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QueryTimeoutException(javax.persistence.QueryTimeoutException) FutureTask(java.util.concurrent.FutureTask) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 4 with QueryTimeoutException

use of javax.persistence.QueryTimeoutException in project hibernate-orm by hibernate.

the class ExceptionConverterImpl method convert.

@Override
public RuntimeException convert(HibernateException e, LockOptions lockOptions) {
    Throwable cause = e;
    if (cause instanceof StaleStateException) {
        final PersistenceException converted = wrapStaleStateException((StaleStateException) cause);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof LockingStrategyException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.exception.LockTimeoutException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.PessimisticLockException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.QueryTimeoutException) {
        final QueryTimeoutException converted = new QueryTimeoutException(cause.getMessage(), cause);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof ObjectNotFoundException) {
        final EntityNotFoundException converted = new EntityNotFoundException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.NonUniqueObjectException) {
        final EntityExistsException converted = new EntityExistsException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.NonUniqueResultException) {
        final NonUniqueResultException converted = new NonUniqueResultException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof UnresolvableObjectException) {
        final EntityNotFoundException converted = new EntityNotFoundException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof QueryException) {
        return new IllegalArgumentException(cause);
    } else if (cause instanceof MultipleBagFetchException) {
        return new IllegalArgumentException(cause);
    } else if (cause instanceof TransientObjectException) {
        try {
            sharedSessionContract.markForRollbackOnly();
        } catch (Exception ne) {
            // we do not want the subsequent exception to swallow the original one
            log.unableToMarkForRollbackOnTransientObjectException(ne);
        }
        // Spec 3.2.3 Synchronization rules
        return new IllegalStateException(e);
    } else {
        final PersistenceException converted = new PersistenceException(cause);
        handlePersistenceException(converted);
        return converted;
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) TransientObjectException(org.hibernate.TransientObjectException) HibernateException(org.hibernate.HibernateException) LockingStrategyException(org.hibernate.dialect.lock.LockingStrategyException) EntityNotFoundException(javax.persistence.EntityNotFoundException) EntityExistsException(javax.persistence.EntityExistsException) LockTimeoutException(javax.persistence.LockTimeoutException) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException) NoResultException(javax.persistence.NoResultException) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) SQLException(java.sql.SQLException) NonUniqueResultException(javax.persistence.NonUniqueResultException) JDBCException(org.hibernate.JDBCException) EntityNotFoundException(javax.persistence.EntityNotFoundException) StaleStateException(org.hibernate.StaleStateException) OptimisticEntityLockException(org.hibernate.dialect.lock.OptimisticEntityLockException) LockingStrategyException(org.hibernate.dialect.lock.LockingStrategyException) EntityExistsException(javax.persistence.EntityExistsException) OptimisticLockException(javax.persistence.OptimisticLockException) StaleObjectStateException(org.hibernate.StaleObjectStateException) PessimisticLockException(javax.persistence.PessimisticLockException) PessimisticEntityLockException(org.hibernate.dialect.lock.PessimisticEntityLockException) TransientObjectException(org.hibernate.TransientObjectException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) QueryTimeoutException(javax.persistence.QueryTimeoutException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) HibernateException(org.hibernate.HibernateException) QueryException(org.hibernate.QueryException) QueryTimeoutException(javax.persistence.QueryTimeoutException) QueryException(org.hibernate.QueryException) StaleStateException(org.hibernate.StaleStateException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) PersistenceException(javax.persistence.PersistenceException) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) LockTimeoutException(javax.persistence.LockTimeoutException) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException)

Aggregations

QueryTimeoutException (javax.persistence.QueryTimeoutException)4 Test (org.junit.Test)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2 FutureTask (java.util.concurrent.FutureTask)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 EntityExistsException (javax.persistence.EntityExistsException)2 EntityNotFoundException (javax.persistence.EntityNotFoundException)2 LockTimeoutException (javax.persistence.LockTimeoutException)2 NoResultException (javax.persistence.NoResultException)2 NonUniqueResultException (javax.persistence.NonUniqueResultException)2 OptimisticLockException (javax.persistence.OptimisticLockException)2 PessimisticLockException (javax.persistence.PessimisticLockException)2 Query (javax.persistence.Query)2 RequiresDialect (org.hibernate.testing.RequiresDialect)2 RequiresDialectFeature (org.hibernate.testing.RequiresDialectFeature)2 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 PersistenceException (javax.persistence.PersistenceException)1 RollbackException (javax.persistence.RollbackException)1