Search in sources :

Example 21 with TransactionException

use of org.osgi.service.transaction.control.TransactionException 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)

Example 22 with TransactionException

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

the class TransactionContextTest method testLastParticipantFailsSoRollback.

@Test
public void testLastParticipantFailsSoRollback() throws Exception {
    ctx.registerLocalResource(localResource);
    ctx.registerXAResource(xaResource, null);
    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(xaResource).rollback(Mockito.any(Xid.class));
    ctx.finish();
    ArgumentCaptor<Xid> captor = ArgumentCaptor.forClass(Xid.class);
    InOrder inOrder = Mockito.inOrder(xaResource, localResource);
    inOrder.verify(xaResource).start(captor.capture(), Mockito.eq(XAResource.TMNOFLAGS));
    inOrder.verify(xaResource).setTransactionTimeout(Mockito.anyInt());
    inOrder.verify(xaResource).end(Mockito.eq(captor.getValue()), Mockito.eq(XAResource.TMSUCCESS));
    inOrder.verify(xaResource).prepare(captor.getValue());
    inOrder.verify(localResource).commit();
    inOrder.verify(xaResource).rollback(Mockito.eq(captor.getValue()));
    Mockito.verifyNoMoreInteractions(xaResource, localResource);
}
Also used : Xid(javax.transaction.xa.Xid) TransactionException(org.osgi.service.transaction.control.TransactionException) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 23 with TransactionException

use of org.osgi.service.transaction.control.TransactionException 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

TransactionException (org.osgi.service.transaction.control.TransactionException)23 Test (org.junit.Test)10 SQLException (java.sql.SQLException)8 DataSource (javax.sql.DataSource)6 ScopedWorkException (org.osgi.service.transaction.control.ScopedWorkException)6 XADataSource (javax.sql.XADataSource)4 DriverDataSource (org.apache.aries.tx.control.jdbc.common.impl.DriverDataSource)4 Configuration (org.osgi.service.cm.Configuration)4 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)4 TransactionContext (org.osgi.service.transaction.control.TransactionContext)4 Connection (java.sql.Connection)3 ResultSet (java.sql.ResultSet)3 Message (org.apache.aries.tx.control.itests.entity.Message)3 XADataSourceMapper (org.apache.aries.tx.control.jdbc.xa.connection.impl.XADataSourceMapper)3 LocalResource (org.osgi.service.transaction.control.LocalResource)3 HikariDataSource (com.zaxxer.hikari.HikariDataSource)2 EntityManager (javax.persistence.EntityManager)2 PersistenceException (javax.persistence.PersistenceException)2 ScopedConnectionWrapper (org.apache.aries.tx.control.jdbc.common.impl.ScopedConnectionWrapper)2 TxConnectionWrapper (org.apache.aries.tx.control.jdbc.common.impl.TxConnectionWrapper)2