Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-50011

Can a transaction used again after document save fails using that transaction?

    • Type: Icon: Question Question
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Let's imagine a situation like this.

      I need to generate 10 documents in the same collection. So I use a loop. The loop won't stop until 10 documents are saved. Even if a single iteration fails, loop continues until the total number of saved documents are 10. This works fine. Then I added transactions to scenario. Let's say transaction starts before the loop executes inside a try catch block. If any error occurs transaction fails except errors in the loop. Because they are ignore to complete the loop until 10 documents are saved. So the problem occurs when transaction is applied to a document when saving. If the loop runs without errors there won't be any problem. But I purposely add a duplicate record on iteration 5 by using a counter. 

      What I expected was to complete the loop and commit the transaction ignoring the duplicate record as the loop however completes with 10 saved documents.

      But when a manual error is introduced using a duplicate record from iteration 6 a MongoDB error is thrown. 'MongoError: Transaction 1 has been aborted.\n' So the loops runs infinite until I add a error counter and throw error out of the loop when error counter reaches a given count.

      So what is wrong in this situation. My guess is, as the document save fails in the iteration 5 due to the duplicate record, the transaction aborts at that point. So the transaction is not available for the iteration 6 as it is already aborted. I don't know weather this is the default behavior of MongoDB. Please verify and is there any way to go around this?

      I'm using NodeJS and Mongoose. Function is attached as a text file. Thank you!

       

            Assignee:
            jonathan.streets@mongodb.com Jonathan Streets (Inactive)
            Reporter:
            sajithneyo94@gmail.com Sajith Neyomal
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: