use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project OpenClinica by OpenClinica.
the class ImportSpringJob method executeInternal.
@Override
protected void executeInternal(final JobExecutionContext context) throws JobExecutionException {
ApplicationContext appContext;
try {
appContext = (ApplicationContext) context.getScheduler().getContext().get("applicationContext");
TransactionTemplate transactionTemplate = (TransactionTemplate) appContext.getBean("sharedTransactionTemplate");
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
executeInternalInTransaction(context);
}
});
} catch (SchedulerException e) {
throw new JobExecutionException(e);
}
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class JpaTransactionManagerTests method testInvalidIsolation.
@Test
public void testInvalidIsolation() {
tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
given(manager.isOpen()).willReturn(true);
try {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
}
});
fail("Should have thrown InvalidIsolationLevelException");
} catch (InvalidIsolationLevelException ex) {
// expected
}
verify(manager).close();
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithIllegalStateExceptionOnRollbackOnly.
@Test
public void jtaTransactionManagerWithIllegalStateExceptionOnRollbackOnly() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
willThrow(new IllegalStateException("no existing transaction")).given(ut).setRollbackOnly();
try {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
status.setRollbackOnly();
}
});
fail("Should have thrown TransactionSystemException");
} catch (TransactionSystemException ex) {
// expected
}
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method doTestJtaTransactionManagerWithNoExceptionOnGlobalRollbackOnly.
private void doTestJtaTransactionManagerWithNoExceptionOnGlobalRollbackOnly(boolean failEarly) throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_MARKED_ROLLBACK, Status.STATUS_MARKED_ROLLBACK, Status.STATUS_MARKED_ROLLBACK);
JtaTransactionManager tm = newJtaTransactionManager(ut);
if (failEarly) {
tm.setFailEarlyOnGlobalRollbackOnly(true);
}
TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
boolean outerTransactionBoundaryReached = false;
try {
assertTrue("Is new transaction", ts.isNewTransaction());
TransactionTemplate tt = new TransactionTemplate(tm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCompletion(int status) {
assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_ROLLED_BACK);
}
});
}
});
outerTransactionBoundaryReached = true;
tm.commit(ts);
fail("Should have thrown UnexpectedRollbackException");
} catch (UnexpectedRollbackException ex) {
// expected
if (!outerTransactionBoundaryReached) {
tm.rollback(ts);
}
if (failEarly) {
assertFalse(outerTransactionBoundaryReached);
} else {
assertTrue(outerTransactionBoundaryReached);
}
}
verify(ut).begin();
if (failEarly) {
verify(ut).rollback();
} else {
verify(ut).commit();
}
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExisting.
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExisting() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
TransactionManager tm = mock(TransactionManager.class);
Transaction tx = mock(Transaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
given(tm.suspend()).willReturn(tx);
JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(ut).begin();
verify(ut).commit();
verify(tm).resume(tx);
}
Aggregations