commitTransaction
Definition
New in version 4.0.
Saves the changes made by the operations in the multi-documenttransaction and ends the transaction.
To run the commitTransaction
, the command must be runagainst the admin
database and run within aSession
. Rather than run the commitTransaction
command directly, most users should use the driver method or themongo
shell Session.commitTransaction()
helper.
The command has the following syntax:
- {
- commitTransaction: 1,
- txnNumber: <long>,
- writeConcern: <document>,
- autocommit: false
- }
Behavior
Write Concern
When committing the transaction, the session uses the write concernspecified at the transaction start. SeeSession.startTransaction()
.
If you commit using "w: 1"
write concern,your transaction can be rolled back if there is a failover..
Atomicity
When a transaction commits, all data changes made in the transactionare saved and visible outside the transaction. That is, a transactionwill not commit some of its changes while rolling back others.
Until a transaction commits, the data changes made in thetransaction are not visible outside the transaction.
However, when a transaction writes to multiple shards, not alloutside read operations need to wait for the result of the committedtransaction to be visible across the shards. For example, if atransaction is committed and write 1 is visible on shard A but write2 is not yet visible on shard B, an outside read at read concern"local"
can read the results of write 1 withoutseeing write 2.