'Mongodb abortTransaction() not working On version 5.0.8
I have the code block below for a MongoDB transaction:
return new Promise(async (resolve, reject) => {
let client = await this.mongoClient();
const transactionSession = client.startSession();
const transactionOptions = {
readPreference: 'primary',
readConcern: { level: 'snapshot' },
writeConcern: { w: 'majority' }
};
const db = client.db("XXX");
try {
const transactionResults = await transactionSession.withTransaction(async () => {
const A = await db.collection("users").updateOne({query}, {$set: {set}}, {transactionSession}).catch(console.error);
const B = await db.collection(this.sessionCollection).updateMany({query}, {$set: {set}}}, {transactionSession}).catch(console.error);
console.log(A, B)
await transactionSession.abortTransaction();
}, transactionOptions);
if (transactionResults) {
console.log("The reservation was successfully created.");
} else {
console.log("The transaction was intentionally aborted.");
return reject({code: 5000});
}
} catch(e){
console.log("The transaction was aborted due to an unexpected error: " + e);
} finally {
await transactionSession.endSession();
}
});
This block code logs "The transaction was intentionally aborted.", but both A and B execute on database!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
