Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-1843

Bulk operation executes outside provided session transaction

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.0.0, 3.6.11
    • Affects Version/s: 3.1.12
    • Component/s: Shell
    • 5
    • Not Needed

      Hello,

      Bulk operation created via initializeOrderedBulkOp or initializeUnorderedBulkOp does not support MongoDB transactions. According to the documentation a client session (with started transaction) can be passed as an option to the collection methods mentioned above, however created bulk operations do not seem to be executed within a passed session transaction. Please review the test case below (also added to the issue attachments).

      "use strict";
      
      const { expect } = require ('chai')
      const { MongoClient } = require ('mongodb')
      
      let client = undefined
      
      describe ('Bulk operation transaction rollback', () => {
      
          before (async () => client = await MongoClient.connect ('mongodb://localhost:27017', { useNewUrlParser: true }))
          after (() => client.close ())
      
          it ('should abort ordered/unordered bulk operation writes', async () => {
      
              const collection = client.db ('Test').collection ('bulk_write_transaction_test')
              await collection.deleteMany ({})
      
              const session = client.startSession ()
              session.startTransaction ({
                  readConcern: { level: 'snapshot' },
                  writeConcern: { w: 'majority' },
              })
      
              let bulk = undefined
      
              // 1.
              bulk = collection.initializeUnorderedBulkOp ({ session })
              bulk.insert ({ 'answer': 42 })
              await bulk.execute ()
      
              // 2.
              bulk = collection.initializeOrderedBulkOp ({ session })
              bulk.insert ({ 'answer': 43 })
              await bulk.execute ()
      
              await session.abortTransaction ()
              await session.endSession ()
      
              const documents = await collection.find ().toArray ()
      
              expect (documents).to.be.lengthOf (0, 'bulk operation writes were made outside of transaction')
      
              return
          })
      })
      

       

            Assignee:
            neal.beeken@mongodb.com Neal Beeken
            Reporter:
            takatan Den
            Daria Pardue
            Votes:
            2 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: