Search in sources :

Example 1 with TransactionStatus

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);
}
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 2 with TransactionStatus

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

Example 3 with TransactionStatus

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);
    }
}
Also used : TransactionException(org.osgi.service.transaction.control.TransactionException) TransactionStatus(org.osgi.service.transaction.control.TransactionStatus) TransactionException(org.osgi.service.transaction.control.TransactionException) SystemException(javax.transaction.SystemException) XAException(javax.transaction.xa.XAException)

Example 4 with TransactionStatus

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());
}
Also used : ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) TransactionStatus(org.osgi.service.transaction.control.TransactionStatus) BindException(java.net.BindException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) BindException(java.net.BindException) Test(org.junit.Test)

Example 5 with TransactionStatus

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());
}
Also used : ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) TransactionStatus(org.osgi.service.transaction.control.TransactionStatus) AtomicReference(java.util.concurrent.atomic.AtomicReference) ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) BindException(java.net.BindException) Test(org.junit.Test)

Aggregations

TransactionStatus (org.osgi.service.transaction.control.TransactionStatus)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 BindException (java.net.BindException)12 Test (org.junit.Test)12 ScopedWorkException (org.osgi.service.transaction.control.ScopedWorkException)12 XAResource (javax.transaction.xa.XAResource)2 TransactionException (org.osgi.service.transaction.control.TransactionException)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 XAConnection (javax.sql.XAConnection)1 SystemException (javax.transaction.SystemException)1 XAException (javax.transaction.xa.XAException)1 AbstractTransactionContextImpl (org.apache.aries.tx.control.service.common.impl.AbstractTransactionContextImpl)1 ServiceEvent (org.osgi.framework.ServiceEvent)1 ServiceListener (org.osgi.framework.ServiceListener)1 LocalResource (org.osgi.service.transaction.control.LocalResource)1 TransactionContext (org.osgi.service.transaction.control.TransactionContext)1