Search in sources :

Example 31 with Xid

use of javax.transaction.xa.Xid in project aries by apache.

the class TransactionContextTest method testXAResourcePreCommitException.

@Test
public void testXAResourcePreCommitException() throws Exception {
    ctx.registerXAResource(xaResource, null);
    Mockito.doAnswer(i -> {
        assertEquals(ROLLING_BACK, ctx.getTransactionStatus());
        return null;
    }).when(xaResource).rollback(Mockito.any(Xid.class));
    ctx.preCompletion(() -> {
        throw new IllegalArgumentException();
    });
    ctx.finish();
    ArgumentCaptor<Xid> captor = ArgumentCaptor.forClass(Xid.class);
    InOrder inOrder = Mockito.inOrder(xaResource);
    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.TMFAIL));
    inOrder.verify(xaResource).rollback(Mockito.eq(captor.getValue()));
    Mockito.verifyNoMoreInteractions(xaResource);
}
Also used : Xid(javax.transaction.xa.Xid) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 32 with Xid

use of javax.transaction.xa.Xid in project geode by apache.

the class XAResourceAdaptor method testXAExceptionRollback.

@Test
public void testXAExceptionRollback() throws Exception {
    utx.begin();
    Thread thread = Thread.currentThread();
    Transaction txn = (Transaction) tm.getTransactionMap().get(thread);
    txn.registerSynchronization(new Synchronization() {

        public void beforeCompletion() {
            fail("Notify Before Completion should not be called in rollback");
        }

        public void afterCompletion(int status) {
            assertTrue(status == Status.STATUS_ROLLEDBACK);
        }
    });
    txn.enlistResource(new XAResourceAdaptor() {

        public void commit(Xid arg0, boolean arg1) throws XAException {
        }

        public void rollback(Xid arg0) throws XAException {
            throw new XAException(6);
        }
    });
    try {
        utx.rollback();
        fail("The rollback should have thrown SystemException");
    } catch (SystemException expected) {
    // success
    }
    assertTrue(tm.getGlobalTransactionMap().isEmpty());
}
Also used : Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 33 with Xid

use of javax.transaction.xa.Xid in project wildfly by wildfly.

the class TransactionInflowResourceAdapter method endpointActivation.

public void endpointActivation(MessageEndpointFactory endpointFactory, ActivationSpec spec) throws ResourceException {
    Xid xid = TransactionInflowXid.getUniqueXid(42);
    TransactionInflowWork work = new TransactionInflowWork(endpointFactory, MSG);
    TransactionContext txnCtx = new TransactionContext();
    txnCtx.setXid(xid);
    TransactionInflowWorkListener workListener = new TransactionInflowWorkListener();
    try {
        bootstrapContext.getWorkManager().startWork(work, WorkManager.IMMEDIATE, txnCtx, workListener);
    } catch (WorkException e) {
        throw new IllegalStateException("Can't start work " + work + " with txn " + txnCtx);
    }
    // start Work blocks until the execution starts but not until its completion
    // timeout 10 seconds
    int timeout = TimeoutUtil.adjust(10_000);
    long start = System.currentTimeMillis();
    while (!workListener.isCompleted() && (System.currentTimeMillis() - start < timeout)) {
        // active waiting
        Thread.yield();
    }
    if (!workListener.isCompleted())
        throw new IllegalStateException("Work " + work + " of xid " + xid + " does not finish.");
    try {
        bootstrapContext.getXATerminator().prepare(xid);
        // depends on value in spec we commit or roll-back
        TransactionInflowRaSpec activationSpec = (TransactionInflowRaSpec) spec;
        if (activationSpec.getAction().equals(ACTION_COMMIT)) {
            bootstrapContext.getXATerminator().commit(xid, false);
        } else if (activationSpec.getAction().equals(ACTION_ROLLBACK)) {
            bootstrapContext.getXATerminator().rollback(xid);
        } else {
            new IllegalStateException("Spec '" + activationSpec + "' defines unknown action");
        }
    } catch (XAException xae) {
        throw new IllegalStateException("Can't process prepare/commit/rollback calls for xid: " + xid, xae);
    }
}
Also used : Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) TransactionContext(javax.resource.spi.work.TransactionContext) WorkException(javax.resource.spi.work.WorkException)

Example 34 with Xid

use of javax.transaction.xa.Xid in project warn-report by saaavsaaa.

the class DistributeTransaction method createXID.

Xid[] createXID() {
    Xid xid_1 = null;
    byte[] gid_1 = new byte[1];
    byte[] bid_1 = new byte[1];
    gid_1[0] = (Byte.decode(props.getProperty("xid.global"))).byteValue();
    bid_1[0] = (Byte.decode(props.getProperty("xid.branch.db_1"))).byteValue();
    System.out.print("Creating an XID (" + Byte.toString(gid_1[0]) + ", " + Byte.toString(bid_1[0]) + ") for DB_1: ");
    xid_1 = new MysqlXid(gid_1, bid_1, 0);
    System.out.println("Okay.");
    Xid xid_2 = null;
    byte[] gid_2 = new byte[1];
    byte[] bid_2 = new byte[1];
    gid_2[0] = (Byte.decode(props.getProperty("xid.global"))).byteValue();
    bid_2[0] = (Byte.decode(props.getProperty("xid.branch.db_2"))).byteValue();
    System.out.print("Creating an XID (" + Byte.toString(gid_2[0]) + ", " + Byte.toString(bid_2[0]) + ") for DB_2: ");
    xid_2 = new MysqlXid(gid_2, bid_2, 0);
    System.out.println("Okay.");
    return new Xid[] { xid_1, xid_2 };
}
Also used : MysqlXid(com.mysql.jdbc.jdbc2.optional.MysqlXid) Xid(javax.transaction.xa.Xid) MysqlXid(com.mysql.jdbc.jdbc2.optional.MysqlXid)

Example 35 with Xid

use of javax.transaction.xa.Xid in project spanner-jdbc by olavloite.

the class XATester method testXATransaction.

private void testXATransaction(CloudSpannerXAConnection xaConnection, CommitMode mode) throws SQLException, XAException {
    log.info("Starting XA simple transaction test");
    Connection connection = xaConnection.getConnection();
    Xid xid = getRandomXid();
    xaConnection.start(xid, XAResource.TMNOFLAGS);
    String sql = "insert into test (id, uuid, active, amount, description, created_date, last_updated) values (?, ?, ?, ?, ?, ?, ?)";
    PreparedStatement statement = connection.prepareStatement(sql);
    setParameterValues(statement, 1000000);
    statement.executeUpdate();
    xaConnection.end(xid, XAResource.TMSUCCESS);
    xaConnection.prepare(xid);
    if (mode != CommitMode.None) {
        xaConnection.commit(xid, mode == CommitMode.OnePhase);
    }
    if (mode != CommitMode.None) {
        boolean found = false;
        try (ResultSet rs = connection.createStatement().executeQuery("select * from test where id=1000000")) {
            if (rs.next())
                found = true;
        }
        Assert.assertTrue(found);
    }
    log.info("Finished XA simple transaction test");
}
Also used : RecoveredXid(nl.topicus.jdbc.xa.RecoveredXid) Xid(javax.transaction.xa.Xid) Connection(java.sql.Connection) CloudSpannerXAConnection(nl.topicus.jdbc.xa.CloudSpannerXAConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Xid (javax.transaction.xa.Xid)82 Test (org.junit.Test)35 XAException (javax.transaction.xa.XAException)20 IOException (java.io.IOException)16 XAResource (javax.transaction.xa.XAResource)14 UnitTest (nl.topicus.jdbc.test.category.UnitTest)11 XidImpl (org.neo4j.kernel.impl.transaction.XidImpl)11 LinkedList (java.util.LinkedList)10 InOrder (org.mockito.InOrder)6 HashMap (java.util.HashMap)5 RecoveredXid (nl.topicus.jdbc.xa.RecoveredXid)5 RelationshipType (org.neo4j.graphdb.RelationshipType)5 HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)4 ArrayList (java.util.ArrayList)4 RollbackException (javax.transaction.RollbackException)4 SystemException (javax.transaction.SystemException)4 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)4 XaResource (org.neo4j.kernel.impl.transaction.xaframework.XaResource)4 TransactionContext (com.hazelcast.transaction.TransactionContext)3 SerializableXID (com.hazelcast.transaction.impl.xa.SerializableXID)3