mongodb - mongodb 在 commitTransaction 失败后我需要 abortTransaction 吗?
问题描述
猫鼬/mongodb node.js 代码:
session.commitTransaction(function(err, reply){
if(err) {
session.abortTransaction(); //Do I need this abort?
}
}
有没有人可以帮助我lz。
解决方案
查看有关事务的 mongodb文档及其提供的示例。
您应该session.abortTransaction();
在数据库请求出现异常后使用。
commit
但是拒绝后就不需要了。
那说猫鼬没有abortTransactions()
。所以我想这不是必需的。
// Runs the txnFunc and retries if TransientTransactionError encountered
function runTransactionWithRetry(txnFunc, session) {
while (true) {
try {
txnFunc(session); // performs transaction
break;
} catch (error) {
// If transient error, retry the whole transaction
if ( error.hasOwnProperty("errorLabels") && error.errorLabels.includes("TransientTransactionError") ) {
print("TransientTransactionError, retrying transaction ...");
continue;
} else {
throw error;
}
}
}
}
// Retries commit if UnknownTransactionCommitResult encountered
function commitWithRetry(session) {
while (true) {
try {
session.commitTransaction(); // Uses write concern set at transaction start.
print("Transaction committed.");
break;
} catch (error) {
// Can retry commit
if (error.hasOwnProperty("errorLabels") && error.errorLabels.includes("UnknownTransactionCommitResult") ) {
print("UnknownTransactionCommitResult, retrying commit operation ...");
continue;
} else {
print("Error during commit ...");
throw error;
}
}
}
}
// Performs inserts and count in a transaction
function updateEmployeeInfo(session) {
employeesCollection = session.getDatabase("hr").employees;
eventsCollection = session.getDatabase("reporting").events;
// Start a transaction for the session that uses:
// - read concern "snapshot"
// - write concern "majority"
session.startTransaction( { readConcern: { level: "snapshot" }, writeConcern: { w: "majority" } } );
try{
eventsCollection.insertOne(
{ employee: 3, status: { new: "Active", old: null }, department: { new: "XYZ", old: null } }
);
// Count number of events for employee 3
var countDoc = eventsCollection.aggregate( [ { $match: { employee: 3 } }, { $count: "eventCounts" } ] ).next();
print( "events count (in active transaction): " + countDoc.eventCounts );
// The following operations should fail as an employee ``3`` already exist in employees collection
employeesCollection.insertOne(
{ employee: 3, name: { title: "Miss", name: "Terri Bachs" }, status: "Active", department: "XYZ" }
);
} catch (error) {
print("Caught exception during transaction, aborting.");
session.abortTransaction();
throw error;
}
commitWithRetry(session);
} // End of updateEmployeeInfo function
// Start a session.
session = db.getMongo().startSession( { mode: "primary" } );
try{
runTransactionWithRetry(updateEmployeeInfo, session);
} catch (error) {
// Do something with error
} finally {
session.endSession();
}
推荐阅读
- azure - 在 Azure 中使用不同的 nic 创建多个 VM
- ios - 当数组具有相似元素时合并数组
- r - 饼图中的标签、颜色和切片之间的错误同步
- assembly - 使用 MASM 的汇编语言中的中缀到后缀转换(对于 x86 处理器)
- c# - 使用c#从字符串中删除转义字符
- javascript - Ember 快速入门教程:解析错误:“@action”中出现意外字符“@”
- list - 如何永远在列表中追加一个元素?
- python - 在 PyQt5 中创建小部件时使用 self 有什么意义
- javascript - 如何获取数组中的每个第n个数字?不行
- java - 使用集合处理 Gson 中的类型检查