0

It's not clear from Spring AMQP documentation on transactions if it's possible to do everything in one transaction: listen to RabbitMQ, receive a message from queue 1, do processing (including persistence) and publish message to queue 2. And if any of these steps fails, all operations in the transaction roll back (including database transaction rollback, rollback on publishing to queue 2 and rollback on receiving from queue 1 - requeuing). Can this work and how can it be implemented in code? For example, we make listener from queue 1 transactional and do data processing and publishing to queue 2 from this listener?

Can this transaction provide exactly once delivery guarantee?

AndCode
  • 384
  • 1
  • 10

1 Answers1

1

See https://docs.spring.io/spring-amqp/docs/current/reference/html/#transactions

As long as the listener container and template are transactional, the work will be performed in one transaction.

You can also synchronize a database transaction with it; however, it is not atomic (XA), it is "Best effort 1PC". See https://www.infoworld.com/article/2077963/distributed-transactions-in-spring--with-and-without-xa.html

No; exactly once is not possible, it is at least once; you will have to deal with possible duplicates.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Thank you @GaryRussell. Is this transaction synchronous or asynchronous and if there can be both options, what's the difference in implementation? Also my understanding is that this transaction automatically handles delivery acks to RabbitMQ and publish confirms from RabbitMQ automatically, and transaction is successful (committed) if delivery was acked and publish confirm received in the transaction. – AndCode Jul 19 '23 at 23:20
  • Synchronous; no, RabbitMQ does not allow transactions and publisher confirms on the same channel. The success or failure is detected on the transaction commit. – Gary Russell Jul 20 '23 at 13:30