Details
-
Bug
-
Resolution: Works as Designed
-
Major - P3
-
None
-
1.3.2
-
None
Description
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.
Attachments
Issue Links
- mentioned in
-
Page Loading...