Search in sources :

Example 46 with Transaction

use of org.apache.activemq.artemis.core.transaction.Transaction in project activemq-artemis by apache.

the class ServerSessionImpl method doRollback.

private void doRollback(final boolean clientFailed, final boolean lastMessageAsDelived, final Transaction theTx) throws Exception {
    boolean wasStarted = started;
    List<MessageReference> toCancel = new ArrayList<>();
    for (ServerConsumer consumer : consumers.values()) {
        if (wasStarted) {
            consumer.setStarted(false);
        }
        toCancel.addAll(consumer.cancelRefs(clientFailed, lastMessageAsDelived, theTx));
    }
    // we add them to a new tx and roll them back as the calling client will assume that this has happened.
    if (theTx.getState() == State.ROLLEDBACK) {
        Transaction newTX = newTransaction();
        cancelAndRollback(clientFailed, newTX, wasStarted, toCancel);
    } else {
        cancelAndRollback(clientFailed, theTx, wasStarted, toCancel);
    }
}
Also used : Transaction(org.apache.activemq.artemis.core.transaction.Transaction) ArrayList(java.util.ArrayList) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) ServerConsumer(org.apache.activemq.artemis.core.server.ServerConsumer)

Example 47 with Transaction

use of org.apache.activemq.artemis.core.transaction.Transaction in project activemq-artemis by apache.

the class ServerSessionImpl method acknowledge.

@Override
public void acknowledge(final long consumerID, final long messageID) throws Exception {
    ServerConsumer consumer = findConsumer(consumerID);
    if (tx != null && tx.getState() == State.ROLLEDBACK) {
        // JBPAPP-8845 - if we let stuff to be acked on a rolled back TX, we will just
        // have these messages to be stuck on the limbo until the server is restarted
        // The tx has already timed out, so we need to ack and rollback immediately
        Transaction newTX = newTransaction();
        try {
            consumer.acknowledge(newTX, messageID);
        } catch (Exception e) {
            // just ignored
            // will log it just in case
            logger.debug("Ignored exception while acking messageID " + messageID + " on a rolledback TX", e);
        }
        newTX.rollback();
    } else {
        consumer.acknowledge(autoCommitAcks ? null : tx, messageID);
    }
}
Also used : Transaction(org.apache.activemq.artemis.core.transaction.Transaction) ServerConsumer(org.apache.activemq.artemis.core.server.ServerConsumer) ActiveMQXAException(org.apache.activemq.artemis.core.exception.ActiveMQXAException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException) XAException(javax.transaction.xa.XAException) ActiveMQNonExistentQueueException(org.apache.activemq.artemis.api.core.ActiveMQNonExistentQueueException) ActiveMQIOErrorException(org.apache.activemq.artemis.api.core.ActiveMQIOErrorException)

Example 48 with Transaction

use of org.apache.activemq.artemis.core.transaction.Transaction in project activemq-artemis by apache.

the class RefsOperation method afterRollback.

@Override
public void afterRollback(final Transaction tx) {
    Map<QueueImpl, LinkedList<MessageReference>> queueMap = new HashMap<>();
    long timeBase = System.currentTimeMillis();
    // add any already acked refs, this means that they have been transferred via a producer.send() and have no
    // previous state persisted.
    List<MessageReference> ackedRefs = new ArrayList<>();
    for (MessageReference ref : refsToAck) {
        ref.emptyConsumerID();
        if (logger.isTraceEnabled()) {
            logger.trace("rolling back " + ref);
        }
        try {
            if (ref.isAlreadyAcked()) {
                ackedRefs.add(ref);
            }
            rollbackRedelivery(tx, ref, timeBase, queueMap);
        } catch (Exception e) {
            ActiveMQServerLogger.LOGGER.errorCheckingDLQ(e);
        }
    }
    for (Map.Entry<QueueImpl, LinkedList<MessageReference>> entry : queueMap.entrySet()) {
        LinkedList<MessageReference> refs = entry.getValue();
        QueueImpl queue = entry.getKey();
        synchronized (queue) {
            queue.postRollback(refs);
        }
    }
    if (!ackedRefs.isEmpty()) {
        // message references
        try {
            Transaction ackedTX = new TransactionImpl(storageManager);
            for (MessageReference ref : ackedRefs) {
                Message message = ref.getMessage();
                if (message.isDurable()) {
                    int durableRefCount = message.incrementDurableRefCount();
                    if (durableRefCount == 1) {
                        storageManager.storeMessageTransactional(ackedTX.getID(), message);
                    }
                    Queue queue = ref.getQueue();
                    storageManager.storeReferenceTransactional(ackedTX.getID(), queue.getID(), message.getMessageID());
                    ackedTX.setContainsPersistent();
                }
                message.incrementRefCount();
            }
            ackedTX.commit(true);
        } catch (Exception e) {
            ActiveMQServerLogger.LOGGER.failedToProcessMessageReferenceAfterRollback(e);
        }
    }
}
Also used : Message(org.apache.activemq.artemis.api.core.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) LinkedList(java.util.LinkedList) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) HashMap(java.util.HashMap) Map(java.util.Map) Queue(org.apache.activemq.artemis.core.server.Queue)

Example 49 with Transaction

use of org.apache.activemq.artemis.core.transaction.Transaction in project activemq-artemis by apache.

the class SimpleOpenWireTest method testXAResourceRolledBackRemoved.

@Test
public void testXAResourceRolledBackRemoved() throws Exception {
    Queue queue = null;
    Xid xid = newXID();
    try (XAConnection xaconnection = xaFactory.createXAConnection()) {
        XASession session = xaconnection.createXASession();
        queue = session.createQueue(queueName);
        session.getXAResource().start(xid, XAResource.TMNOFLAGS);
        MessageProducer producer = session.createProducer(queue);
        producer.send(session.createTextMessage("xa message"));
        session.getXAResource().end(xid, XAResource.TMSUCCESS);
        session.getXAResource().rollback(xid);
    }
    XidImpl xid1 = new XidImpl(xid);
    Transaction transaction = server.getResourceManager().getTransaction(xid1);
    assertNull(transaction);
}
Also used : Xid(javax.transaction.xa.Xid) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) XASession(javax.jms.XASession) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) MessageProducer(javax.jms.MessageProducer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) XAConnection(javax.jms.XAConnection) Test(org.junit.Test)

Example 50 with Transaction

use of org.apache.activemq.artemis.core.transaction.Transaction in project activemq-artemis by apache.

the class PagingCounterTest method testCleanupCounter.

@Test
public void testCleanupCounter() throws Exception {
    ClientSessionFactory sf = createSessionFactory(sl);
    ClientSession session = sf.createSession();
    try {
        server.addAddressInfo(new AddressInfo(new SimpleString("A1"), RoutingType.ANYCAST));
        Queue queue = server.createQueue(new SimpleString("A1"), RoutingType.ANYCAST, new SimpleString("A1"), null, true, false);
        PageSubscriptionCounter counter = locateCounter(queue);
        StorageManager storage = server.getStorageManager();
        Transaction tx = new TransactionImpl(server.getStorageManager());
        for (int i = 0; i < 2100; i++) {
            counter.increment(tx, 1, 1000);
            if (i % 200 == 0) {
                tx.commit();
                storage.waitOnOperations();
                assertEquals(i + 1, counter.getValue());
                assertEquals((i + 1) * 1000, counter.getPersistentSize());
                tx = new TransactionImpl(server.getStorageManager());
            }
        }
        tx.commit();
        storage.waitOnOperations();
        assertEquals(2100, counter.getValue());
        assertEquals(2100 * 1000, counter.getPersistentSize());
        server.stop();
        server = newActiveMQServer();
        server.start();
        queue = server.locateQueue(new SimpleString("A1"));
        assertNotNull(queue);
        counter = locateCounter(queue);
        assertEquals(2100, counter.getValue());
        assertEquals(2100 * 1000, counter.getPersistentSize());
    } finally {
        sf.close();
        session.close();
    }
}
Also used : Transaction(org.apache.activemq.artemis.core.transaction.Transaction) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) StorageManager(org.apache.activemq.artemis.core.persistence.StorageManager) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) PageSubscriptionCounter(org.apache.activemq.artemis.core.paging.cursor.PageSubscriptionCounter) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) Queue(org.apache.activemq.artemis.core.server.Queue) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo) 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