5.8. Using Transactions in C++
This section shows how to use server local transactions, which buffer published messages and acknowledgements and process them upon commit, guaranteeing that they will all succeed or fail as a unit. In AMQP you can do this by making the session transactional. Once you do this, all message transfers and acknowledgements are queued up until a commit or rollback is done on the session. After a commit or rollback, the session remains transactional, so operations continue to be queued up until the next commit or rollback.
To make a session transactional, call tx_select():
session.txSelect()
To commit all operations pending on a transactional session, call tx_commit():
session.txCommit()
To roll back all operations pending on a transactional session, call tx_rollback():
session.txRollback()
Transactions are used primarily to ensure that delivery is kept consistent in a messaging system. For instance, if you want to make sure that messages are properly forwarded, you can make a session transactional, subscribe to one queue, and publish received messages to another queue, acknowledging the initial delivery and doing a commit. If you do this, the publish and consume are atomic, and will both succeed or fail as a unit.