Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-1721

(NoSuchTransaction) Transaction 1 has been aborted.

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 1.3.2
    • Component/s: CRUD
    • None

      My Aim: In a transaction, I want to replace a doc manually instead of aborting the transaction automatically after inserting a doc failed because of `duplicate key error`.

      The `insert or replace code` shows like below:

       

      // InsertContract insert contract
      func InsertContract(ctx context.Context, contract *models.Contract) (err error) {
         ...
         res, err = collection.InsertOne(c, contract)
         if err != nil {
            log.Error("insert one contract error:%+v, result:%+v", err, res)
            if strings.Contains(err.Error(), consts.MongoDocumentExisted) {
               filter = findOneContractFilter(contract.ContractId, contract.ProductId)
               _, err = collection.ReplaceOne(c, filter, contract)
               if err != nil {
                  log.Error("replace after insert one contract error:%+v, filter:%+v", err, *filter)
               }
            }
            return
         }
         ...
         return
      }
      

      The transaction code shows like below:

       

       

      if transaction := config.Pool.UseSessionWithOptions(ctx, &options.SessionOptions{
         CausalConsistency:     &options.DefaultCausalConsistency,
         DefaultReadPreference: readpref.Primary(),
      }, func(sessionContext mongo.SessionContext) error {
         // start session
         if err = sessionContext.StartTransaction(); err != nil {
            return err
         }
         if err = mypkg.InsertContract(sessionContext, contract); err != nil {
            return sessionContext.AbortTransaction(sessionContext)
         }
      ....

      But it doesn't work.

       

      The log print below: 

      [E] insert one contract error:multiple write errors: [{write errors: [{E11000 duplicate key error collection: ...
      [E] replace after insert one contract error:(NoSuchTransaction) Transaction 1 has been aborted....
      

      The Transaction always aborts automatically, cause that I cannot replace the doc manually.

       

            Assignee:
            isabella.siu@mongodb.com Isabella Siu (Inactive)
            Reporter:
            1150555483@qq.com 汇嘉 庄
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: