Transactions¶
The Tx
or transaction class implements transactional functionality with RabbitMQ and allows for any AMQP command to be issued, then committed or rolled back.
It can be used as a normal Python object:
with rabbitpy.Connection() as connection:
with connection.channel() as channel:
tx = rabbitpy.Tx(channel)
tx.select()
exchange = rabbitpy.Exchange(channel, 'my-exchange')
exchange.declare()
tx.commit()
Or as a context manager (See PEP 0343) where the transaction will automatically be started and committed for you:
with rabbitpy.Connection() as connection:
with connection.channel() as channel:
with rabbitpy.Tx(channel) as tx:
exchange = rabbitpy.Exchange(channel, 'my-exchange')
exchange.declare()
In the event of an exception exiting the block when used as a context manager, the transaction will be rolled back for you automatically.
API Documentation¶
-
class
rabbitpy.
Tx
(channel)[source]¶ Work with transactions
The Tx class allows publish and ack operations to be batched into atomic units of work. The intention is that all publish and ack requests issued within a transaction will complete successfully or none of them will. Servers SHOULD implement atomic transactions at least where all publish or ack requests affect a single queue. Transactions that cover multiple queues may be non-atomic, given that queues can be created and destroyed asynchronously, and such events do not form part of any transaction. Further, the behaviour of transactions with respect to the immediate and mandatory flags on Basic.Publish methods is not defined.
Parameters: channel ( rabbitpy.channel.Channel
) – The channel object to start the transaction on-
commit
()[source]¶ Commit the current transaction
This method commits all message publications and acknowledgments performed in the current transaction. A new transaction starts immediately after a commit.
Raises: rabbitpy.exceptions.NoActiveTransactionError Return type: bool
-
rollback
()[source]¶ Abandon the current transaction
This method abandons all message publications and acknowledgments performed in the current transaction. A new transaction starts immediately after a rollback. Note that unacked messages will not be automatically redelivered by rollback; if that is required an explicit recover call should be issued.
Raises: rabbitpy.exceptions.NoActiveTransactionError Return type: bool
-