On this page本页内容
The Callback API:回调API:
The Core API:核心API:
The new callback API incorporates logic:新的回调API包含以下逻辑:
Important
insert
or update operations with upsert: true
) must be on existing collections if run inside transactions.The example uses the new callback API for working with transactions, which starts a transaction, executes the specified operations, and commits (or aborts on error). 该示例使用新的回调API处理事务,它启动事务,执行指定的操作,并提交(或在出错时中止)。The new callback API incorporates retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors.新的回调API包含"TransientTransactionError"或"UnknownTransactionCommitResult"提交错误的重试逻辑。
Important
insert
or update operations with upsert: true
) must be on existing collections if run inside transactions.upsert:true
的插入或更新操作)必须在现有集合上。The example uses the new callback API for working with transactions, which starts a transaction, executes the specified operations, and commits (or aborts on error). 该示例使用新的回调API处理事务,它启动事务,执行指定的操作,并提交(或在出错时中止)。The new callback API incorporates retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors.新的回调API包含"TransientTransactionError"或"UnknownTransactionCommitResult"提交错误的重试逻辑。
Important
insert
or update operations with upsert: true
) must be on existing collections if run inside transactions.The example uses the new callback API for working with transactions, which starts a transaction, executes the specified operations, and commits (or aborts on error). The new callback API incorporates retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors.
Important
insert
or update operations with upsert: true
) must be on existing collections if run inside transactions.The example uses the new callback API for working with transactions, which starts a transaction, executes the specified operations, and commits (or aborts on error). The new callback API incorporates retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors.
Important
insert
or update operations with upsert: true
)
must be on existing collections if run inside transactions.The example uses the new callback API for working with transactions, which starts a transaction, executes the specified operations, and commits (or aborts on error). The new callback API incorporates retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors.
Important
insert
or update operations with upsert: true
) must be on existing collections if run inside transactions.upsert:true
的插入或更新操作)必须在现有集合上。The example uses the new callback API for working with transactions, which starts a transaction, executes the specified operations, and commits (or aborts on error). 该示例使用新的回调API处理事务,它启动事务,执行指定的操作,并提交(或在出错时中止)。The new callback API incorporates retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors.
Important
insert
or update operations with upsert: true
) must be on existing collections if run inside transactions.upsert:true
的插入或更新操作)必须在现有集合上。The example uses the new callback API for working with transactions, which starts a transaction, executes the specified operations, and commits (or aborts on error). 该示例使用新的回调API处理事务,它启动事务,执行指定的操作,并提交(或在出错时中止)。The new callback API incorporates retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors.新的回调API包含"TransientTransactionError"或"UnknownTransactionCommitResult"提交错误的重试逻辑。
Important重要的
insert
or update operations with upsert: true
) must be on existing collections if run inside transactions.upsert:true
的插入或更新操作)必须在现有集合上。The example uses the new callback API for working with transactions, which starts a transaction, executes the specified operations, and commits (or aborts on error). 该示例使用新的回调API处理事务,它启动事务,执行指定的操作,并提交(或在出错时中止)。The new callback API incorporates retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors.
Important
insert
or update operations with upsert: true
) must be on existing collections if run inside transactions.The example uses the new callback API for working with transactions, which starts a transaction, executes the specified operations, and commits (or aborts on error). The new callback API incorporates retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors.
Note
For the Scala driver, see the Core API usage example instead.
The example uses the new callback API for working with transactions, which starts a transaction, executes the specified operations, and commits (or aborts on error). The new callback API incorporates retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors.
Important
insert
or update operations with upsert: true
)
must be on existing collections if run inside transactions.The core transaction API does not incorporate retry logic for errors labeled:
To handle "TransientTransactionError", applications should explicitly incorporate retry logic for the error.
To handle "UnknownTransactionCommitResult", applications should explicitly incorporate retry logic for the error.
The following example incorporates logic to retry the transaction for transient errors and retry the commit for unknown commit error:
Important
To associate read and write operations with a transaction, you must pass the session to each operation in the transaction.
Important
To associate read and write operations with a transaction, you must pass the session to each operation in the transaction.
Important
To associate read and write operations with a transaction, you must pass the session to each operation in the transaction.
Important
To associate read and write operations with a transaction, you must pass the session to each operation in the transaction.
Note
For Motor, see the Callback API instead.
Important
To associate read and write operations with a transaction, you must pass the session to each operation in the transaction.
Important
To associate read and write operations with a transaction, you must pass the session to each operation in the transaction.
Important
To associate read and write operations with a transaction, you must pass the session to each operation in the transaction.
For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2:
For transactions on MongoDB 4.0 replica sets, clients require MongoDB drivers updated for MongoDB 4.0 or later.
|
|
|
Regardless of the database system, whether MongoDB or relational databases, applications should take measures to handle errors during transaction commits and incorporate retry logic for transactions.无论数据库系统是MongoDB还是关系数据库,应用程序都应该采取措施来处理事务提交期间的错误,并为事务合并重试逻辑。
"TransientTransactionError"
¶The individual write operations inside the transaction are not retryable, regardless of the value of 无论retryWrites
. retryWrites
的值如何,事务中的单个写入操作都不可重试。If an operation encounters an error associated with the label "TransientTransactionError"
, such as when the primary steps down, the transaction as a whole can be retried.
"TransientTransactionError"
."TransientTransactionError"
. To handle "TransientTransactionError"
, applications should explicitly incorporate retry logic for the error."UnknownTransactionCommitResult"
¶The commit operations are retryable write operations. If the commit operation encounters an error, MongoDB drivers retry the commit regardless of the value of retryWrites
.
If the commit operation encounters an error labeled 如果提交操作遇到标记为"UnknownTransactionCommitResult"
, the commit can be retried."UnknownTransactionCommitResult"
的错误,则可以重试提交。
"UnknownTransactionCommitResult"
."UnknownTransactionCommitResult"
. To handle "UnknownTransactionCommitResult"
, applications should explicitly incorporate retry logic for the error.On sharded clusters with multiple mongos
instances, performing transactions with drivers updated for MongoDB 4.0 (instead of MongoDB 4.2) will fail and can result in errors, including:
Note
Your driver may return a different error. Refer to your driver’s documentation for details.
Error Code | Error Message |
---|---|
251 | cannot continue txnId -1 for session ... with txnId 1 |
50940 | cannot commit with no participants |
For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), use the MongoDB drivers updated for MongoDB 4.2
mongo
Shell Example¶The following mongo
shell methods are available for transactions:
Note
The mongo
shell example omits retry logic and robust error handling for simplicity’s sake. For a more practical example of incorporating transactions in applications, see Transaction Error Handling instead.