Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(schema): avoid applying default write concern to operations that are in a transaction #14391

Merged
merged 1 commit into from Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
});
});