0

I worked with mongo driver for golang, but possibly that question is actual for other implementations.

Do mongo driver always abort transaction in case errors? Can I prevent implicit abort for transactions?

For example, for such code, I always get err2 = (NoSuchTransaction) Transaction 7 has been aborted. if err1!= nil.

    client := ir.Source.Client()
    session, err := client.StartSession()
    if err != nil {
        return err
    }

    if err := session.StartTransaction(); err != nil {
        return err
    }
    if err = mongo.WithSession(ctx, session, func(sc mongo.SessionContext) error {

        _, err1 := ir.Source.Collection(collectionName).UpdateOne(sc,
            bson.D{{Key: "_id", Value: bid}},
            bson.M{
                "$set": bson.D{{Key: "name", Value: "Name"}},
            },
        )
        if err1 != nil {
            log.Println(err1) // I don`t want abort here
        }
        _, err2 = ir.Source.Collection("collectionName").UpdateOne(sc,
            bson.D{{Key: "_id", Value: bid}},
            bson.M{
                "$set": bson.D{{Key: "name", Value: "Name"}},
            },
        )
        if err2 != nil {
            log.Println(err2) 
            sc.AbortTransaction(sc) // i want abort only here
        }
        return sc.CommitTransaction(sc)
     }); err!=nil {
  return err
}

Can I use some options for transactions or rewrite code, so I will control transactions abort by myself?

Ragnar
  • 141
  • 2
  • 15

0 Answers0