Docs Menu
Docs Home
/
MongoDB Manual
/ / /

commitTransaction

On this page

  • Definition
  • Behavior
commitTransaction

New in version 4.0.

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

Tip

In the mongo Shell, this command can also be run through the Session.commitTransaction() helper method.

Helper methods are convenient for mongo users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.

To run the commitTransaction, the command must be run against the admin database and run within a Session().

The command has the following syntax:

{
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 "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

On this page