Prakash
Prakash

Reputation: 482

How to prevent TransactionRolledBackException in ActiveMQ

We have a consumer (C# application) that subscribes to a queue of ActiveMQ Classic. We are using AcknowledgementMode.Transactional to create the session.

Connection URI:

activemq:failover://(tcp://broker1.com:61780)?randomize=false&transport.maxReconnectAttempts=3&transport.reconnectDelay=3000&connection.prefetchPolicy.queuePrefetch=1

There are many instances this application running and each is functioning well.

Receives the message in the consumer app which sends ack (_session.Commit()) to the broker. This process has been running with no issue until recently where our network has some intermittent glitches.

On such cases, the AMQ broker is throwing the TransactionRolledBackException from the method _session.Commit()

Here is the full stack trace:

Apache.NMS.TransactionRolledBackException: Transaction completion in doubt due to failover. Forcing rollback of ID:qcyp-slack-03-64154-638666766860410819-1:3826:5
   at Apache.NMS.ActiveMQ.Connection.SyncRequest(Command command, TimeSpan requestTimeout)
   at Apache.NMS.ActiveMQ.TransactionContext.Commit()
   at Apache.NMS.ActiveMQ.Session.DoCommit()
   at MyCompany.ActiveMQ.ActiveMqClient.Commit()
   at MyCompany.Core.Messaging.Pooling.AvailableMessageQueueClient.Commit()

This issue is intermittent. From our research and analysis of our logs, it is found that when we get this TransactionRolledBackException, the broker rolled back the transaction and hence that same message will be delivered to another consumer.

Now, my question is how we can prevent such an exception or how can we make our application more resilient to such issues?

Upvotes: 0

Views: 39

Answers (1)

Justin Bertram
Justin Bertram

Reputation: 35122

The only way to completely prevent transaction rollbacks is to ensure the operating environment never has any problems (e.g. network glitches) and your application never has any bugs (e.g. crashes while processing a message).

However, that's a completely unrealistic goal. There will always be problems, and, in fact, this is one of the main reasons transactions were created in the first place. A transaction rollback is a feature, not a bug. It is a graceful way to handle a failure, and your application should be able to deal with it. In theory, your application should be able to consume the message again and suffer no ill effects.

Upvotes: 0

Related Questions