mongodb - MongoDB 控制的事务中止
问题描述
我与 golang 的 mongo 驱动程序一起工作,但对于其他实现来说,这个问题可能是实际的。
如果出现错误,mongo 驱动程序是否总是中止事务?我可以防止交易的隐式中止吗?
例如,对于这样的代码,我总是得到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
}
我可以使用一些选项进行交易或重写代码,以便我自己控制交易中止吗?
解决方案
推荐阅读
- python - 将 tkinter Optionmenu 的标题设置为字符串和 StringVar
- android - 蓝牙与 DisplayOnly 配对不出现密码框
- html - Chrome 剪切长页面
- node.js - NodeJS 注册 - 用户名区分大小写(唯一字段)
- intellij-idea - 无法解析另一个模块中的类的符号
- python - Google Groups Settings API 400“无效值”错误
- docker - docker 容器是如何构建的?
- c++ - 为什么数组中对象的析构函数在被另一个对象替换时不被调用?
- php - PHP - 获取不大于下一个数组值的数组值
- c# - WPF子菜单覆盖而不是展开