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())));
}
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);
}
Aggregations