Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
fix(transactions): write concern is always majority on txn retry
Browse files Browse the repository at this point in the history
NODE-1848
  • Loading branch information
mbroadst committed Jan 29, 2019
1 parent a02d5bb commit 7b240ea
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,19 @@ function endTransaction(session, commandName, callback) {
const command = { [commandName]: 1 };

// apply a writeConcern if specified
let writeConcern;
if (session.transaction.options.writeConcern) {
Object.assign(command, { writeConcern: session.transaction.options.writeConcern });
writeConcern = Object.assign({}, session.transaction.options.writeConcern);
} else if (session.clientOptions && session.clientOptions.w) {
Object.assign(command, { writeConcern: { w: session.clientOptions.w } });
writeConcern = { w: session.clientOptions.w };
}

if (txnState === TxnState.TRANSACTION_COMMITTED) {
writeConcern = Object.assign({ wtimeout: 10000 }, writeConcern, { w: 'majority' });
}

if (writeConcern) {
Object.assign(command, { writeConcern });
}

function commandHandler(e, r) {
Expand Down Expand Up @@ -342,6 +351,13 @@ function endTransaction(session, commandName, callback) {
// send the command
session.topology.command('admin.$cmd', command, { session }, (err, reply) => {
if (err && isRetryableError(err)) {
// SPEC-1185: apply majority write concern when retrying commitTransaction
if (command.commitTransaction) {
command.writeConcern = Object.assign({ wtimeout: 10000 }, command.writeConcern, {
w: 'majority'
});
}

return session.topology.command('admin.$cmd', command, { session }, (_err, _reply) =>
commandHandler(transactionError(_err), _reply)
);
Expand Down

0 comments on commit 7b240ea

Please sign in to comment.