Search in sources :

Example 6 with PessimisticLockException

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

the class TestPersistenceExceptionSubclassFilterRetryPredicate method testNestedFilteredJPAExceptions.

@Test
public void testNestedFilteredJPAExceptions() {
    assertFalse(predicate.test(wrapCause(new EntityExistsException())));
    assertFalse(predicate.test(wrapCause(new EntityNotFoundException())));
    assertFalse(predicate.test(wrapCause(new LockTimeoutException())));
    assertFalse(predicate.test(wrapCause(new NoResultException())));
    assertFalse(predicate.test(wrapCause(new NonUniqueResultException())));
    assertFalse(predicate.test(wrapCause(new OptimisticLockException())));
    assertFalse(predicate.test(wrapCause(new PessimisticLockException())));
    assertFalse(predicate.test(wrapCause(new QueryTimeoutException())));
    assertFalse(predicate.test(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 7 with PessimisticLockException

use of javax.persistence.PessimisticLockException in project droolsjbpm-integration by kiegroup.

the class PessimisticLockingSpringTest method testPessimisticLock.

@Test
public void testPessimisticLock() throws Exception {
    RuntimeManager manager = getManager();
    final AbstractPlatformTransactionManager transactionManager = getTransactionManager();
    AuditLogService logService = getLogService();
    final DefaultTransactionDefinition defTransDefinition = new DefaultTransactionDefinition();
    final List<Exception> exceptions = new ArrayList<Exception>();
    RuntimeEngine engine = getEngine();
    final KieSession ksession = getKieSession();
    final ProcessInstance processInstance = ksession.startProcess(HUMAN_TASK_PROCESS_ID);
    final ProcessInstanceStatus abortedProcessInstanceStatus = new ProcessInstanceStatus();
    final CountDownLatch txAcquiredSignal = new CountDownLatch(1);
    final CountDownLatch pessLockExceptionSignal = new CountDownLatch(1);
    final CountDownLatch threadsAreDoneLatch = new CountDownLatch(2);
    Thread t1 = new Thread() {

        @Override
        public void run() {
            TransactionStatus status = transactionManager.getTransaction(defTransDefinition);
            log.debug("Attempting to abort to lock process instance for 3 secs ");
            // getProcessInstance does not lock reliably so let's make a change that actually does something to the entity
            ksession.abortProcessInstance(processInstance.getId());
            // let thread 2 start once we have the transaction
            txAcquiredSignal.countDown();
            try {
                // keep the lock until thread 2 let's us know it's done
                pessLockExceptionSignal.await();
            } catch (InterruptedException e) {
            // do nothing
            }
            log.debug("Commited process instance aborting after 3 secs");
            transactionManager.commit(status);
            // let main test thread know we're done
            threadsAreDoneLatch.countDown();
        }
    };
    // Trying to retrieve process instance in second thread.
    // Should throw PessimisticLockException because we are trying to get write lock on process instance which already have lock
    Thread t2 = new Thread() {

        @Override
        public void run() {
            try {
                // wait for thread 1 to tell us it has the lock
                txAcquiredSignal.await();
            } catch (InterruptedException e) {
            // do nothing
            }
            log.debug("Trying to get process instance - should fail because process instance is locked or wait until thread 1 finish and return null because process instance is deleted.");
            try {
                ProcessInstance abortedProcessInstance = ksession.getProcessInstance(processInstance.getId(), true);
                if (abortedProcessInstance == null) {
                    abortedProcessInstanceStatus.setAbortedProcessInstance(true);
                }
                log.debug("Get request worked well");
            } catch (Exception e) {
                log.debug("Get request failed with error {}", e.getMessage());
                exceptions.add(e);
            } finally {
                // Tell thread 1 that we're done
                pessLockExceptionSignal.countDown();
            }
            // let main test thread know we're done
            threadsAreDoneLatch.countDown();
        }
    };
    t1.start();
    t2.start();
    // wait for both threads to finish!
    threadsAreDoneLatch.await();
    // Therefore exception list should be empty.
    if (abortedProcessInstanceStatus.isAbortedProcessInstance()) {
        assertEquals(0, exceptions.size());
    } else {
        // Otherwise database transaction timeout should be lower than waiting time set and thread 2 should throw PessimisticLockException or
        // LockTimeoutException.
        assertEquals(1, exceptions.size());
        assertThat(exceptions.get(0).getClass().getName(), anyOf(equalTo(PessimisticLockException.class.getName()), equalTo(LockTimeoutException.class.getName())));
    }
    TransactionStatus status = transactionManager.getTransaction(defTransDefinition);
    ProcessInstanceLog instanceLog = logService.findProcessInstance(processInstance.getId());
    transactionManager.commit(status);
    assertNotNull(instanceLog);
    assertEquals(ProcessInstance.STATE_ABORTED, instanceLog.getStatus().intValue());
    manager.disposeRuntimeEngine(engine);
}
Also used : AuditLogService(org.jbpm.process.audit.AuditLogService) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) CountDownLatch(java.util.concurrent.CountDownLatch) LockTimeoutException(javax.persistence.LockTimeoutException) PessimisticLockException(javax.persistence.PessimisticLockException) PessimisticLockException(javax.persistence.PessimisticLockException) AbstractPlatformTransactionManager(org.springframework.transaction.support.AbstractPlatformTransactionManager) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) LockTimeoutException(javax.persistence.LockTimeoutException) ProcessInstanceLog(org.kie.api.runtime.manager.audit.ProcessInstanceLog) Test(org.junit.Test)

Aggregations

PessimisticLockException (javax.persistence.PessimisticLockException)7 LockTimeoutException (javax.persistence.LockTimeoutException)5 Test (org.junit.Test)4 PersistenceException (javax.persistence.PersistenceException)3 EntityManager (com.haulmont.cuba.core.EntityManager)2 EntityManager (javax.persistence.EntityManager)2 OptimisticLockException (javax.persistence.OptimisticLockException)2 QueryTimeoutException (javax.persistence.QueryTimeoutException)2 Session (org.hibernate.Session)2 RequiresDialectFeature (org.hibernate.testing.RequiresDialectFeature)2 BigDecimal (java.math.BigDecimal)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Callable (java.util.concurrent.Callable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 EntityExistsException (javax.persistence.EntityExistsException)1