use of org.osgi.service.transaction.control.TransactionStatus 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.TransactionStatus in project aries by apache.
the class TransactionContextImpl method preCompletion.
@Override
public void preCompletion(Runnable job) throws IllegalStateException {
TransactionStatus status = getTransactionStatus();
if (status.compareTo(MARKED_ROLLBACK) > 0) {
throw new IllegalStateException("The current transaction is in state " + status);
}
preCompletion.add(job);
}
use of org.osgi.service.transaction.control.TransactionStatus in project aries by apache.
the class TransactionContextImpl method registerXAResource.
@Override
public void registerXAResource(XAResource resource, String name) {
TransactionStatus status = getTransactionStatus();
if (status.compareTo(MARKED_ROLLBACK) > 0) {
throw new IllegalStateException("The current transaction is in state " + status);
}
try {
if (name == null) {
currentTransaction.enlistResource(resource);
} else {
NamedXAResourceImpl res = new NamedXAResourceImpl(name, resource, transactionManager, true);
postCompletion(x -> res.close());
currentTransaction.enlistResource(res);
}
} catch (Exception e) {
throw new TransactionException("The transaction was unable to enlist a resource", e);
}
}
use of org.osgi.service.transaction.control.TransactionStatus in project aries by apache.
the class TransactionControlRunningTest method testTwoRequiredsNestedNoRollbackForInnerException.
@Test
public void testTwoRequiredsNestedNoRollbackForInnerException() {
AtomicReference<TransactionStatus> finalStatusOuter = new AtomicReference<>();
AtomicReference<TransactionStatus> finalStatusInner = new AtomicReference<>();
Exception userEx = new BindException("Bang!");
try {
txControl.required(() -> {
assertTrue(txControl.activeTransaction());
Object key = txControl.getCurrentContext().getTransactionKey();
txControl.getCurrentContext().postCompletion(finalStatusOuter::set);
try {
txControl.build().noRollbackFor(BindException.class).requiresNew(() -> {
assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
txControl.getCurrentContext().postCompletion(finalStatusInner::set);
throw userEx;
});
fail("Should not be reached!");
} catch (ScopedWorkException swe) {
throw swe.as(BindException.class);
}
return null;
});
fail("Should not be reached!");
} catch (ScopedWorkException swe) {
assertSame(userEx, swe.getCause());
}
assertEquals(ROLLED_BACK, finalStatusOuter.get());
assertEquals(COMMITTED, finalStatusInner.get());
}
use of org.osgi.service.transaction.control.TransactionStatus in project aries by apache.
the class TransactionControlRunningTest method testRequiredUserException.
@Test
public void testRequiredUserException() {
AtomicReference<TransactionStatus> finalStatus = new AtomicReference<>();
Exception userEx = new Exception("Bang!");
try {
txControl.required(() -> {
assertTrue(txControl.activeTransaction());
txControl.getCurrentContext().postCompletion(finalStatus::set);
throw userEx;
});
fail("Should not be reached");
} catch (ScopedWorkException swe) {
assertSame(userEx, swe.getCause());
}
assertEquals(ROLLED_BACK, finalStatus.get());
}
Aggregations