Thanks for providing more information and code snippets.
It is likely that you’re not seeing the full error message because at the end of function updateRequestCronJob
the catch
does not throw error. The full error message should look something similar as below:
{ MongoError: Given transaction number 3 does not match any in-progress transactions. The active transaction number is 2
at Connection.<anonymous> (/vbox/mongo/nodejs/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:450:61)
at Connection.emit (events.js:182:13)
at processMessage (/vbox/mongo/nodejs/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:384:10)
at Socket.<anonymous> (/vbox/mongo/nodejs/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:586:15)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onread (net.js:639:20)
errorLabels: [ 'TransientTransactionError' ],
operationTime:
Timestamp { _bsontype: 'Timestamp', low_: 8, high_: 1582506018 },
ok: 0,
errmsg:
'Given transaction number 3 does not match any in-progress transactions. The active transaction number is 2',
code: 251,
codeName: 'NoSuchTransaction',
'$clusterTime':
{ clusterTime:
Timestamp { _bsontype: 'Timestamp', low_: 14, high_: 1582506018 },
signature: { hash: [Binary], keyId: 0 } },
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]: {} }
This is likely related to mongoose issue #7502, and SERVER-36428.
You can try to incorporate logic to retry the transaction for transient errors, and also retry the commit for unknown commit error. For example:
async function runTransactionWithRetry(txnFunc, data) {
let session = await mongoose.startSession();
session.startTransaction();
try {
await txnFunc(data, session);
} catch (error) {
console.log('Transaction aborted. Caught exception during transaction.');
// If transient error, retry the whole transaction
if (error.errorLabels && error.errorLabels.indexOf('TransientTransactionError') >= 0) {
console.log('TransientTransactionError, retrying transaction ...');
await runTransactionWithRetry(txnFunc, data);
} else {
session.abortTransaction();
console.log("runTransactionWithRetry error: ");
throw error;
}
}
}
async function commitWithRetry(session) {
try {
await session.commitTransaction();
console.log('Transaction committed.');
} catch (error) {
if (
error.errorLabels &&
error.errorLabels.indexOf('UnknownTransactionCommitResult') >= 0
) {
console.log('UnknownTransactionCommitResult, retrying commit operation ...');
await commitWithRetry(session);
} else {
console.log('Error during commit ...');
throw error;
}
}
}
See more information and snippets on Transactions In Applications: Core API (Switch the code tab to Node.JS)
Regards,
Wan.