On this page本页内容
commitTransaction
New in version 4.0.在版本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()
. commitTransaction
,必须对admin
数据库运行该命令,并在Session()
中运行。Rather than run the 大多数用户应该使用驱动程序方法或commitTransaction
command directly, most users should use the driver method or the mongosh
Session.commitTransaction()
helper.mongosh
Session.commitTransaction()
助手,而不是直接运行commitTransaction
命令。
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()
.Session.startTransaction()
。
If you commit using the 如果您使用"w: 1"
write concern, your transaction can be rolled back if there is a failover."w: 1"
写入问题提交,则在发生故障切换时可以回滚事务。
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 例如,如果提交了一个事务,并且写1在分片a上可见,但写2在分片B上尚不可见,则外部读时关注点"local"
can read the results of write 1 without seeing write 2."local"
可以读取写1的结果,而不会看到写2。