{"record":{"id":"361822ad2ec3f1e9","repo":"mongodb/node-mongodb-native","slug":"transactions-do-not-support-unacknowledged-write-c","errorCode":null,"errorMessage":"Transactions do not support unacknowledged write concern","messagePattern":"Transactions do not support unacknowledged write concern","errorType":"exception","errorClass":"MongoTransactionError","httpStatus":null,"severity":"error","filePath":"src/transactions.ts","lineNumber":93,"sourceCode":"/**\n * @internal\n */\nexport class Transaction {\n  state: TxnState;\n  options: TransactionOptions;\n  _pinnedServer?: Server;\n  _recoveryToken?: Document;\n\n  /** Create a transaction */\n  constructor(options?: TransactionOptions) {\n    options = options ?? {};\n    this.state = TxnState.NO_TRANSACTION;\n    this.options = {};\n\n    const writeConcern = WriteConcern.fromOptions(options);\n    if (writeConcern) {\n      if (writeConcern.w === 0) {\n        throw new MongoTransactionError('Transactions do not support unacknowledged write concern');\n      }\n\n      this.options.writeConcern = writeConcern;\n    }\n\n    if (options.readConcern) {\n      this.options.readConcern = ReadConcern.fromOptions(options);\n    }\n\n    if (options.readPreference) {\n      this.options.readPreference = ReadPreference.fromOptions(options);\n    }\n\n    if (options.maxCommitTimeMS) {\n      this.options.maxTimeMS = options.maxCommitTimeMS;\n    }\n\n    // TODO: This isn't technically necessary","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/transactions.ts#L75-L111","documentation":"Thrown by the Transaction constructor when the resolved write concern has w === 0 (unacknowledged). MongoDB transactions require that every operation be acknowledged so the server can report commit/abort status; an unacknowledged write concern makes transaction semantics impossible. The driver rejects this at transaction creation time via MongoTransactionError.","triggerScenarios":"Calling session.withTransaction(callback, { writeConcern: { w: 0 } }) or session.startTransaction({ writeConcern: { w: 0 } }). Also when the MongoClient-level writeConcern is set to w:0 and a transaction is started without overriding it.","commonSituations":"Reusing a fire-and-forget write concern (w:0) from high-throughput insert paths when running transactional code. Copy-pasting client options across contexts where transactional guarantees are needed.","solutions":["Remove w:0 from the transaction options or use an acknowledged write concern (w:1, w:'majority', or omit it).","If the client-level writeConcern is w:0, pass an explicit acknowledged write concern to startTransaction/withTransaction.","Do not use unacknowledged writes for any operation that participates in a transaction."],"exampleFix":"// before\nsession.startTransaction({ writeConcern: { w: 0 } });\n\n// after\nsession.startTransaction({ writeConcern: { w: 'majority' } });","handlingStrategy":"validation","validationCode":"function isAcknowledgedWC(wc: unknown): boolean {\n  if (!wc || typeof wc !== 'object') return true; // default is acknowledged\n  const w = (wc as any).w;\n  return w !== 0 && w !== '0';\n}\nif (!isAcknowledgedWC(txnOptions.writeConcern)) {\n  throw new Error('Refusing to start transaction with unacknowledged write concern');\n}","typeGuard":"function hasAcknowledgedWriteConcern(opts: { writeConcern?: any }): boolean {\n  const w = opts.writeConcern?.w;\n  return w === undefined || w === 1 || w === 'majority' || (typeof w === 'number' && w > 0);\n}","tryCatchPattern":"try {\n  session.startTransaction({ writeConcern: { w: 'majority' } });\n} catch (e) {\n  if (e instanceof MongoTransactionError && /unacknowledged/.test(e.message)) {\n    session.startTransaction(); // retry with default acknowledged write concern\n  } else throw e;\n}","preventionTips":["Never set w:0 on clients/sessions used for transactions.","Keep a separate MongoClient with w:0 for fire-and-forget inserts, distinct from transactional clients.","Validate writeConcern options in a shared helper before starting transactions."],"tags":["transaction","write-concern","validation"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}