use of org.osgi.service.transaction.control.LocalResource in project aries by apache.
the class TransactionContextTest method testMultipleLocalResourcesFirstFailsSoRollback.
@Test
public void testMultipleLocalResourcesFirstFailsSoRollback() throws Exception {
ctx = new TransactionContextImpl(getTxMgr(), true, ENABLED);
ctx.registerLocalResource(localResource);
LocalResource localResource2 = Mockito.mock(LocalResource.class);
ctx.registerLocalResource(localResource2);
Mockito.doAnswer(i -> {
assertEquals(COMMITTING, ctx.getTransactionStatus());
throw new TransactionException("Unable to commit");
}).when(localResource).commit();
Mockito.doAnswer(i -> {
assertEquals(ROLLING_BACK, ctx.getTransactionStatus());
return null;
}).when(localResource2).rollback();
ctx.finish();
Mockito.verify(localResource).commit();
Mockito.verify(localResource2).rollback();
}
use of org.osgi.service.transaction.control.LocalResource in project aries by apache.
the class TransactionContextImpl method finish.
@Override
public void finish() {
beforeCompletion(() -> setRollbackOnly());
TransactionStatus status;
if (getRollbackOnly()) {
vanillaRollback();
status = ROLLED_BACK;
} else {
tranStatus.set(COMMITTING);
List<LocalResource> committed = new ArrayList<>(resources.size());
List<LocalResource> rolledback = new ArrayList<>(0);
resources.stream().forEach(lr -> {
try {
if (getRollbackOnly()) {
lr.rollback();
rolledback.add(lr);
} else {
lr.commit();
committed.add(lr);
}
} catch (Exception e) {
firstUnexpectedException.compareAndSet(null, e);
if (committed.isEmpty()) {
tranStatus.set(ROLLING_BACK);
}
rolledback.add(lr);
}
});
status = tranStatus.updateAndGet(ts -> ts == ROLLING_BACK ? ROLLED_BACK : COMMITTED);
}
afterCompletion(status);
}
use of org.osgi.service.transaction.control.LocalResource in project aries by apache.
the class TransactionContextTest method testLocalResourcesFirstFailsSoRollback.
@Test
public void testLocalResourcesFirstFailsSoRollback() throws Exception {
ctx.registerLocalResource(localResource);
LocalResource localResource2 = Mockito.mock(LocalResource.class);
ctx.registerLocalResource(localResource2);
Mockito.doAnswer(i -> {
assertEquals(COMMITTING, ctx.getTransactionStatus());
throw new TransactionException("Unable to commit");
}).when(localResource).commit();
Mockito.doAnswer(i -> {
assertEquals(ROLLING_BACK, ctx.getTransactionStatus());
return null;
}).when(localResource2).rollback();
ctx.finish();
Mockito.verify(localResource).commit();
Mockito.verify(localResource2).rollback();
}
use of org.osgi.service.transaction.control.LocalResource in project aries by apache.
the class TransactionContextTest method testSecondLocalResourceCannotBeAdded.
@Test
public void testSecondLocalResourceCannotBeAdded() throws Exception {
ctx.registerLocalResource(localResource);
LocalResource localResource2 = Mockito.mock(LocalResource.class);
try {
ctx.registerLocalResource(localResource2);
fail("A second local resource should trigger a failure");
} catch (TransactionException te) {
}
}
Aggregations