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);
}
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());
}
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);
}
}
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 };
}
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");
}
Aggregations