use of org.apache.activemq.artemis.core.transaction.Transaction in project activemq-artemis by apache.
the class ServerSessionImpl method xaFailed.
@Override
public synchronized void xaFailed(final Xid xid) throws Exception {
Transaction theTX = resourceManager.getTransaction(xid);
if (theTX == null) {
theTX = newTransaction(xid);
resourceManager.putTransaction(xid, theTX);
}
if (theTX.isEffective()) {
logger.debug("Client failed with Xid " + xid + " but the server already had it " + theTX.getState());
tx = null;
} else {
theTX.markAsRollbackOnly(new ActiveMQException("Can't commit as a Failover happened during the operation"));
tx = theTX;
}
if (logger.isTraceEnabled()) {
logger.trace("xastart into tx= " + tx);
}
}
use of org.apache.activemq.artemis.core.transaction.Transaction in project activemq-artemis by apache.
the class ServerSessionImpl method xaResume.
@Override
public synchronized void xaResume(final Xid xid) throws Exception {
if (tx != null) {
final String msg = "Cannot resume, session is currently doing work in a transaction " + tx.getXid();
throw new ActiveMQXAException(XAException.XAER_PROTO, msg);
} else {
Transaction theTx = resourceManager.getTransaction(xid);
if (theTx == null) {
final String msg = "Cannot find xid in resource manager: " + xid;
throw new ActiveMQXAException(XAException.XAER_NOTA, msg);
} else {
if (theTx.getState() != Transaction.State.SUSPENDED) {
throw new ActiveMQXAException(XAException.XAER_PROTO, "Cannot resume transaction, it is not suspended " + xid);
} else {
tx = theTx;
tx.resume();
}
}
}
}
use of org.apache.activemq.artemis.core.transaction.Transaction in project activemq-artemis by apache.
the class ServerSessionImpl method markTXFailed.
@Override
public void markTXFailed(Throwable e) {
Transaction currentTX = this.tx;
if (currentTX != null) {
if (e instanceof ActiveMQException) {
currentTX.markAsRollbackOnly((ActiveMQException) e);
} else {
ActiveMQException exception = new ActiveMQException(e.getMessage());
exception.initCause(e);
currentTX.markAsRollbackOnly(exception);
}
}
}
use of org.apache.activemq.artemis.core.transaction.Transaction in project activemq-artemis by apache.
the class TransactionImplTest method testTimeoutAndThenCommitWithARollback.
@Test
public void testTimeoutAndThenCommitWithARollback() throws Exception {
TransactionImpl tx = new TransactionImpl(newXID(), new FakeSM(), 10);
Assert.assertTrue(tx.hasTimedOut(System.currentTimeMillis() + 60000, 10));
final AtomicInteger commit = new AtomicInteger(0);
final AtomicInteger rollback = new AtomicInteger(0);
tx.addOperation(new TransactionOperation() {
@Override
public void beforePrepare(Transaction tx) throws Exception {
}
@Override
public void afterPrepare(Transaction tx) {
}
@Override
public void beforeCommit(Transaction tx) throws Exception {
}
@Override
public void afterCommit(Transaction tx) {
System.out.println("commit...");
commit.incrementAndGet();
}
@Override
public void beforeRollback(Transaction tx) throws Exception {
}
@Override
public void afterRollback(Transaction tx) {
System.out.println("rollback...");
rollback.incrementAndGet();
}
@Override
public List<MessageReference> getRelatedMessageReferences() {
return null;
}
@Override
public List<MessageReference> getListOnConsumer(long consumerID) {
return null;
}
});
for (int i = 0; i < 2; i++) {
try {
tx.commit();
Assert.fail("Exception expected!");
} catch (ActiveMQException expected) {
}
}
// it should just be ignored!
tx.rollback();
Assert.assertEquals(0, commit.get());
Assert.assertEquals(1, rollback.get());
}
use of org.apache.activemq.artemis.core.transaction.Transaction in project activemq-artemis by apache.
the class TransactionImplTest method testTimeoutThenRollbackWithRollback.
@Test
public void testTimeoutThenRollbackWithRollback() throws Exception {
TransactionImpl tx = new TransactionImpl(newXID(), new FakeSM(), 10);
Assert.assertTrue(tx.hasTimedOut(System.currentTimeMillis() + 60000, 10));
final AtomicInteger commit = new AtomicInteger(0);
final AtomicInteger rollback = new AtomicInteger(0);
tx.addOperation(new TransactionOperation() {
@Override
public void beforePrepare(Transaction tx) throws Exception {
}
@Override
public void afterPrepare(Transaction tx) {
}
@Override
public void beforeCommit(Transaction tx) throws Exception {
}
@Override
public void afterCommit(Transaction tx) {
System.out.println("commit...");
commit.incrementAndGet();
}
@Override
public void beforeRollback(Transaction tx) throws Exception {
}
@Override
public void afterRollback(Transaction tx) {
System.out.println("rollback...");
rollback.incrementAndGet();
}
@Override
public List<MessageReference> getRelatedMessageReferences() {
return null;
}
@Override
public List<MessageReference> getListOnConsumer(long consumerID) {
return null;
}
});
tx.rollback();
// This is a case where another failure was detected (In parallel with the TX timeout for instance)
tx.markAsRollbackOnly(new ActiveMQException("rollback only again"));
tx.rollback();
Assert.assertEquals(0, commit.get());
Assert.assertEquals(1, rollback.get());
}
Aggregations