Docs Menu
Docs Home
/
MongoDB Manual
/ / /

commitTransaction

On this page

  • Definition
  • Syntax
  • Behavior
commitTransaction

New in version 4.0.

Saves the changes made by the operations in the multi-document transaction and ends the transaction.

To run the commitTransaction, the command must be run against the admin database and run within a Session(). Rather than run the commitTransaction command directly, most users should use the driver method or the mongosh Session.commitTransaction() helper.

The command has the following syntax:

db.adminCommand(
{
commitTransaction: 1,
txnNumber: <long>,
writeConcern: <document>,
autocommit: false,
comment: <any>
}
)

When committing the transaction, the session uses the write concern specified at the transaction start. See Session.startTransaction().

If you commit using the "w: 1" write concern, your transaction can be rolled back if there is a failover..

When a transaction commits, all data changes made in the transaction are saved and visible outside the transaction. That is, a transaction will not commit some of its changes while rolling back others.

Until a transaction commits, the data changes made in the transaction are not visible outside the transaction.

However, when a transaction writes to multiple shards, not all outside read operations need to wait for the result of the committed transaction to be visible across the shards. For example, if a transaction is committed and write 1 is visible on shard A but write 2 is not yet visible on shard B, an outside read at read concern "local" can read the results of write 1 without seeing write 2.

Back

abortTransaction