Search in sources :

Example 1 with LocalResource

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();
}
Also used : AbstractTransactionContextImpl(org.apache.aries.tx.control.service.common.impl.AbstractTransactionContextImpl) TransactionException(org.osgi.service.transaction.control.TransactionException) LocalResource(org.osgi.service.transaction.control.LocalResource) Test(org.junit.Test)

Example 2 with LocalResource

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);
}
Also used : ACTIVE(org.osgi.service.transaction.control.TransactionStatus.ACTIVE) COMMITTING(org.osgi.service.transaction.control.TransactionStatus.COMMITTING) ROLLED_BACK(org.osgi.service.transaction.control.TransactionStatus.ROLLED_BACK) AbstractTransactionContextImpl(org.apache.aries.tx.control.service.common.impl.AbstractTransactionContextImpl) MARKED_ROLLBACK(org.osgi.service.transaction.control.TransactionStatus.MARKED_ROLLBACK) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ROLLING_BACK(org.osgi.service.transaction.control.TransactionStatus.ROLLING_BACK) Consumer(java.util.function.Consumer) XAResource(javax.transaction.xa.XAResource) COMMITTED(org.osgi.service.transaction.control.TransactionStatus.COMMITTED) List(java.util.List) LocalResource(org.osgi.service.transaction.control.LocalResource) TransactionContext(org.osgi.service.transaction.control.TransactionContext) TransactionStatus(org.osgi.service.transaction.control.TransactionStatus) ArrayList(java.util.ArrayList) TransactionStatus(org.osgi.service.transaction.control.TransactionStatus) LocalResource(org.osgi.service.transaction.control.LocalResource)

Example 3 with LocalResource

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();
}
Also used : TransactionException(org.osgi.service.transaction.control.TransactionException) LocalResource(org.osgi.service.transaction.control.LocalResource) Test(org.junit.Test)

Example 4 with LocalResource

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) {
    }
}
Also used : TransactionException(org.osgi.service.transaction.control.TransactionException) LocalResource(org.osgi.service.transaction.control.LocalResource) Test(org.junit.Test)

Aggregations

LocalResource (org.osgi.service.transaction.control.LocalResource)4 Test (org.junit.Test)3 TransactionException (org.osgi.service.transaction.control.TransactionException)3 AbstractTransactionContextImpl (org.apache.aries.tx.control.service.common.impl.AbstractTransactionContextImpl)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Consumer (java.util.function.Consumer)1 XAResource (javax.transaction.xa.XAResource)1 TransactionContext (org.osgi.service.transaction.control.TransactionContext)1 TransactionStatus (org.osgi.service.transaction.control.TransactionStatus)1 ACTIVE (org.osgi.service.transaction.control.TransactionStatus.ACTIVE)1 COMMITTED (org.osgi.service.transaction.control.TransactionStatus.COMMITTED)1 COMMITTING (org.osgi.service.transaction.control.TransactionStatus.COMMITTING)1 MARKED_ROLLBACK (org.osgi.service.transaction.control.TransactionStatus.MARKED_ROLLBACK)1 ROLLED_BACK (org.osgi.service.transaction.control.TransactionStatus.ROLLED_BACK)1 ROLLING_BACK (org.osgi.service.transaction.control.TransactionStatus.ROLLING_BACK)1