Search in sources :

Example 11 with TransactionStatus

use of org.osgi.service.transaction.control.TransactionStatus in project aries by apache.

the class TransactionContextImpl method postCompletion.

@Override
public void postCompletion(Consumer<TransactionStatus> job) throws IllegalStateException {
    TransactionStatus status = tranStatus.get();
    if (status == COMMITTED || status == ROLLED_BACK) {
        throw new IllegalStateException("The current transaction is in state " + tranStatus);
    }
    postCompletion.add(job);
}
Also used : TransactionStatus(org.osgi.service.transaction.control.TransactionStatus)

Example 12 with TransactionStatus

use of org.osgi.service.transaction.control.TransactionStatus in project aries by apache.

the class TransactionControlRunningTest method testTwoRequiredsNestedOuterThrowsException.

@Test
public void testTwoRequiredsNestedOuterThrowsException() {
    AtomicReference<TransactionStatus> finalStatusOuter = new AtomicReference<>();
    AtomicReference<TransactionStatus> finalStatusInner = new AtomicReference<>();
    Exception userEx = new Exception("Bang!");
    try {
        txControl.required(() -> {
            assertTrue(txControl.activeTransaction());
            Object key = txControl.getCurrentContext().getTransactionKey();
            txControl.getCurrentContext().postCompletion(finalStatusOuter::set);
            txControl.setRollbackOnly();
            txControl.requiresNew(() -> {
                assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
                txControl.getCurrentContext().postCompletion(finalStatusInner::set);
                return null;
            });
            throw userEx;
        });
        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) AtomicReference(java.util.concurrent.atomic.AtomicReference) ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) BindException(java.net.BindException) Test(org.junit.Test)

Example 13 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)

Example 14 with TransactionStatus

use of org.osgi.service.transaction.control.TransactionStatus in project aries by apache.

the class TransactionControlRunningTest method testTwoRequiredsNestedOuterThrowsException.

@Test
public void testTwoRequiredsNestedOuterThrowsException() {
    AtomicReference<TransactionStatus> finalStatusOuter = new AtomicReference<>();
    AtomicReference<TransactionStatus> finalStatusInner = new AtomicReference<>();
    Exception userEx = new Exception("Bang!");
    try {
        txControl.required(() -> {
            assertTrue(txControl.activeTransaction());
            Object key = txControl.getCurrentContext().getTransactionKey();
            txControl.getCurrentContext().postCompletion(finalStatusOuter::set);
            txControl.setRollbackOnly();
            txControl.requiresNew(() -> {
                assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
                txControl.getCurrentContext().postCompletion(finalStatusInner::set);
                return null;
            });
            throw userEx;
        });
        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) AtomicReference(java.util.concurrent.atomic.AtomicReference) ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) BindException(java.net.BindException) Test(org.junit.Test)

Example 15 with TransactionStatus

use of org.osgi.service.transaction.control.TransactionStatus in project aries by apache.

the class TransactionControlRunningTest method testRequiredNoRollbackException.

@Test
public void testRequiredNoRollbackException() {
    AtomicReference<TransactionStatus> finalStatus = new AtomicReference<>();
    Exception userEx = new BindException("Bang!");
    try {
        txControl.build().noRollbackFor(BindException.class).required(() -> {
            assertTrue(txControl.activeTransaction());
            txControl.getCurrentContext().postCompletion(finalStatus::set);
            throw userEx;
        });
        fail("Should not be reached");
    } catch (ScopedWorkException swe) {
        assertSame(userEx, swe.getCause());
    }
    assertEquals(COMMITTED, finalStatus.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)

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