Search in sources :

Example 16 with TransactionStatus

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

the class TransactionControlRunningTest method testTwoRequiredsNestedInnerThrowsException.

@Test
public void testTwoRequiredsNestedInnerThrowsException() {
    AtomicReference<TransactionStatus> finalStatusOuter = new AtomicReference<>();
    AtomicReference<TransactionStatus> finalStatusInner = new AtomicReference<>();
    Exception userEx = new Exception("Bang!");
    txControl.required(() -> {
        assertTrue(txControl.activeTransaction());
        Object key = txControl.getCurrentContext().getTransactionKey();
        txControl.getCurrentContext().postCompletion(finalStatusOuter::set);
        try {
            txControl.requiresNew(() -> {
                assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
                txControl.getCurrentContext().postCompletion(finalStatusInner::set);
                txControl.setRollbackOnly();
                throw userEx;
            });
            fail("Should not be reached!");
        } catch (ScopedWorkException swe) {
            assertSame(userEx, swe.getCause());
        }
        return null;
    });
    assertEquals(COMMITTED, finalStatusOuter.get());
    assertEquals(ROLLED_BACK, 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 17 with TransactionStatus

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

the class TransactionControlRunningTest method testTwoRequiredsNestedInnerThrowsExceptionOuterDoesNotCatch.

@Test
public void testTwoRequiredsNestedInnerThrowsExceptionOuterDoesNotCatch() {
    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.requiresNew(() -> {
                assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
                txControl.getCurrentContext().postCompletion(finalStatusInner::set);
                txControl.setRollbackOnly();
                throw userEx;
            });
            fail("Should not be reached!");
            return null;
        });
    } catch (ScopedWorkException swe) {
        assertSame(userEx, swe.getCause());
    }
    assertEquals(ROLLED_BACK, finalStatusOuter.get());
    assertEquals(ROLLED_BACK, 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 18 with TransactionStatus

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

the class TransactionLogTest method doRecoveryRequired.

public void doRecoveryRequired(BiConsumer<XAResource, XAResource> ordering, TransactionStatus expectedFinalState) throws Exception {
    //Register the recoverable resource
    ArgumentCaptor<ServiceListener> captor = ArgumentCaptor.forClass(ServiceListener.class);
    Mockito.verify(ctx).addServiceListener(captor.capture(), Mockito.anyString());
    Mockito.when(ctx.getService(serviceRef)).thenReturn(new TestRecoverableResource("foo", dataSource));
    captor.getValue().serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, serviceRef));
    XAConnection xaConn = dataSource.getXAConnection();
    AtomicReference<TransactionStatus> ref = new AtomicReference<TransactionStatus>();
    try {
        txControl.required(() -> {
            txControl.getCurrentContext().postCompletion(ref::set);
            Connection conn = xaConn.getConnection();
            // conn.setAutoCommit(false);
            XAResource dsResource = xaConn.getXAResource();
            XAResource poison = Mockito.mock(XAResource.class);
            Mockito.when(poison.prepare(Mockito.any())).thenAnswer(i -> {
                conn.createStatement().execute("shutdown immediately");
                Thread.sleep(1000);
                return XA_OK;
            });
            ordering.accept(dsResource, poison);
            return conn.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )");
        });
    } catch (TransactionException te) {
        assertEquals(expectedFinalState, ref.get());
        assertEquals(expectedFinalState == ROLLED_BACK, te instanceof TransactionRolledBackException);
    } finally {
        try {
            xaConn.close();
        } catch (SQLException sqle) {
        }
    }
    setupServerAndDataSource();
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) SQLException(java.sql.SQLException) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) TransactionStatus(org.osgi.service.transaction.control.TransactionStatus) AtomicReference(java.util.concurrent.atomic.AtomicReference) XAResource(javax.transaction.xa.XAResource) RecoverableXAResource(org.osgi.service.transaction.control.recovery.RecoverableXAResource) TransactionException(org.osgi.service.transaction.control.TransactionException) ServiceEvent(org.osgi.framework.ServiceEvent) TransactionRolledBackException(org.osgi.service.transaction.control.TransactionRolledBackException) XAConnection(javax.sql.XAConnection)

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