Search in sources :

Example 26 with Transaction

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);
    }
}
Also used : Transaction(org.apache.activemq.artemis.core.transaction.Transaction) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException)

Example 27 with Transaction

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();
            }
        }
    }
}
Also used : Transaction(org.apache.activemq.artemis.core.transaction.Transaction) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQXAException(org.apache.activemq.artemis.core.exception.ActiveMQXAException)

Example 28 with Transaction

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);
        }
    }
}
Also used : Transaction(org.apache.activemq.artemis.core.transaction.Transaction) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException)

Example 29 with Transaction

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());
}
Also used : TransactionOperation(org.apache.activemq.artemis.core.transaction.TransactionOperation) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) RouteContextList(org.apache.activemq.artemis.core.server.RouteContextList) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Test(org.junit.Test)

Example 30 with Transaction

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());
}
Also used : TransactionOperation(org.apache.activemq.artemis.core.transaction.TransactionOperation) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) RouteContextList(org.apache.activemq.artemis.core.server.RouteContextList) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Test(org.junit.Test)

Aggregations

Transaction (org.apache.activemq.artemis.core.transaction.Transaction)52 TransactionImpl (org.apache.activemq.artemis.core.transaction.impl.TransactionImpl)24 MessageReference (org.apache.activemq.artemis.core.server.MessageReference)14 Test (org.junit.Test)14 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)13 Xid (javax.transaction.xa.Xid)12 Queue (org.apache.activemq.artemis.core.server.Queue)11 ArrayList (java.util.ArrayList)10 Message (org.apache.activemq.artemis.api.core.Message)10 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)9 Map (java.util.Map)8 StorageManager (org.apache.activemq.artemis.core.persistence.StorageManager)8 HashMap (java.util.HashMap)7 PageSubscriptionCounter (org.apache.activemq.artemis.core.paging.cursor.PageSubscriptionCounter)7 PageSubscription (org.apache.activemq.artemis.core.paging.cursor.PageSubscription)6 BindingsTransactionImpl (org.apache.activemq.artemis.core.transaction.impl.BindingsTransactionImpl)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 List (java.util.List)4 NoSuchElementException (java.util.NoSuchElementException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4