- Working With Data >
- Transactions
Transactions¶
On this page
Version 4.0 of the MongoDB server introduces multi-document transactions. (Updates to multiple fields within a single document are atomic in all versions of MongoDB). Transactions require Mongoid version 6.4 or higher and Ruby driver version 2.6 or higher.
Using Transactions¶
In order to start a transaction, the application must have a session.
A transaction can be started by calling the start_transaction
method on a session, which can be
obtained by calling the with_session
method on either a model class or instance:
It is also possible to specify read concern, write concern and read preference when starting a transaction:
A transaction may be committed or aborted. The corresponding methods to do so are
commit_transaction
and abort_transaction
, again on the session instance:
If a session ends with an open transaction, the transaction is aborted.
The transaction commit can be retried if it fails. Here is the Ruby code to do so:
Note that in order to perform operations within the transaction, operations must use the same client that the session was initiated on. By default, all operations will be done on the default client:
To explicitly use a different client, use the with
method: