Skip to content

Commit

Permalink
Merge pull request #14391 from Automattic/vkarpov15/gh-11382
Browse files Browse the repository at this point in the history
fix(schema): avoid applying default write concern to operations that are in a transaction
  • Loading branch information
vkarpov15 committed Feb 29, 2024
2 parents c02141b + cf4c4f4 commit 4b34967
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/helpers/schema/applyWriteConcern.js
Expand Up @@ -6,6 +6,12 @@ module.exports = function applyWriteConcern(schema, options) {
if (options.writeConcern != null) {
return;
}
// Don't apply default write concern to operations in transactions,
// because setting write concern on an operation in a transaction is an error
// See: https://www.mongodb.com/docs/manual/reference/write-concern/
if (options && options.session && options.session.transaction) {
return;
}
const writeConcern = get(schema, 'options.writeConcern', {});
if (Object.keys(writeConcern).length != 0) {
options.writeConcern = {};
Expand Down
16 changes: 16 additions & 0 deletions test/docs/transactions.test.js
Expand Up @@ -477,4 +477,20 @@ describe('transactions', function() {

assert.equal(i, 3);
});

it('doesnt apply schema write concern to transaction operations (gh-11382)', async function() {
db.deleteModel(/Test/);
const Test = db.model('Test', Schema({ status: String }, { writeConcern: { w: 'majority' } }));

await Test.createCollection();
await Test.deleteMany({});

const session = await db.startSession();

await session.withTransaction(async function() {
await Test.findOneAndUpdate({}, { name: 'test' }, { session });
});

await session.endSession();
});
});

0 comments on commit 4b34967

Please sign in to comment.