开启了一个事务在中间重复提交了,后面又复用了这个事务,导致了BUG,问题就是重复提交了
bash{
"level": "error",
"logTime": "2024-05-13 16:17:23.007",
"logger": "wealthAdmin",
"msg": "create TUserOrderFollowUp failed",
"requestId": "bdef522cb16d5a58a3b2cbfa4d340655",
"context": "POST:/wealthAdmin/v2/CounterfoilBuy",
"uin": "6afaeb7c16be5878b65b5dda1732243c",
"pid": 29160,
"fileLine": "trade\\internal\\admin\\service\\order_create_counterfoil_v2.go:133",
"error": "create error: sql: transaction has already been committed or rolled back",
"errorVerbose": "sql: transaction has already been committed or rolled backcreate error\ngl.fotechwealth.com.local/backend/trade-lib.git/dbaccess.(*accessor).Create\n\tD:/Users/yingjie.huang/go/pkg/mod/gl.fotechwealth.com.local/backend/trade-lib.git/dbaccess@v0.0.0-20221103065121-71047491dbb3/accessor.go:234\ntrade/internal/admin/service.(*wealthAdminService).CounterfoilBuyV2.func1\n\tD:/Work/wealth/internal/admin/service/order_create_counterfoil_v2.go:121\ngorm.io/gorm.(*DB).Transaction\n\tD:/Users/yingjie.huang/go/pkg/mod/gorm.io/gorm@v1.24.5/finisher_api.go:647\ntrade/internal/admin/service.(*wealthAdminService).CounterfoilBuyV2\n\tD:/Work/wealth/internal/admin/service/order_create_counterfoil_v2.go:106\ntrade/internal/admin/controller.(*wealthAdminController).CounterfoilBuyV2\n\tD:/Work/wealth/internal/admin/controller/admin_v2.go:77\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ntrade/middleware/ucuserinfo.Injector.func1\n\tD:/Work/wealth/middleware/ucuserinfo/middleware.go:87\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ntrade/middleware/auth.NewAdminPermission.func1\n\tD:/Work/wealth/middleware/auth/auth.go:422\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ngl.fotechwealth.com.local/backend/trade-lib.git/fiber/middleware/validator.Validator.func1\n\tD:/Users/yingjie.huang/go/pkg/mod/gl.fotechwealth.com.local/backend/trade-lib.git/fiber@v0.0.0-20240124010927-1532ceef9c99/middleware/validator/vaildator.go:38\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ngl.fotechwealth.com.local/backend/trade-lib.git/fiber/middleware/bodyparser.BodyParser.func1\n\tD:/Users/yingjie.huang/go/pkg/mod/gl.fotechwealth.com.local/backend/trade-lib.git/fiber@v0.0.0-20240124010927-1532ceef9c99/middleware/bodyparser/bodyparser.go:22\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ngl.fotechwealth.com.local/backend/trade-lib.git/fiber/middleware/requestparams.RequestParams.func1\n\tD:/Users/yingjie.huang/go/pkg/mod/gl.fotechwealth.com.local/backend/trade-lib.git/fiber@v0.0.0-20240124010927-1532ceef9c99/middleware/requestparams/request_params.go:24\ngithub.com/gofiber/fiber/v2.(*App).next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/router.go:145\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:970\ngl.fotechwealth.com.local/backend/trade-lib.git/apm.New.func1\n\tD:/Users/yingjie.huang/go/pkg/mod/gl.fotechwealth.com.local/backend/trade-lib.git/apm@v0.0.0-20230303093242-a29ea1a36732/apm.go:86\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ngl.fotechwealth.com.local/backend/trade-lib.git/fiber/middleware/trace.Trace.func1\n\tD:/Users/yingjie.huang/go/pkg/mod/gl.fotechwealth.com.local/backend/trade-lib.git/fiber@v0.0.0-20240124010927-1532ceef9c99/middleware/trace/trace.go:120\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ntrade/middleware/requestid.New.func1\n\tD:/Work/wealth/middleware/requestid/requestid.go:51\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ntrade/middleware/recover.New.func1\n\tD:/Work/wealth/middleware/recover/recover.go:42\ngithub.com/gofiber/fiber/v2.(*App).next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/router.go:145\ngithub.com/gofiber/fiber/v2.(*App).handler\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/router.go:172\ngithub.com/valyala/fasthttp.(*Server).serveConn\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/valyala/fasthttp@v1.49.0/server.go:2357\ngithub.com/valyala/fasthttp.(*workerPool).workerFunc\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/valyala/fasthttp@v1.49.0/workerpool.go:224
github.com/valyala/fasthttp.(*workerPool).getCh.func1\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/valyala/fasthttp@v1.49.0/workerpool.go: 196\nruntime.goexit\n\tD:/go/src/runtime/asm_amd64.s: 1594"
}
{
"level": "error",
"logTime": "2024-05-13 16:37:54.960",
"logger": "wealthAdmin",
"msg": "create TUserOrderFollowUp failed",
"requestId": "bdef522cb16d5a59a3b2cbfa4d340656",
"context": "POST:/wealthAdmin/v2/CounterfoilBuy",
"uin": "6afaeb7c16be5878b65b5dda1732243c",
"pid": 29160,
"fileLine": "trade\\internal\\admin\\service\\order_create_counterfoil_v2.go:133",
"error": "create error: sql: transaction has already been committed or rolled back",
"errorVerbose": "sql: transaction has already been committed or rolled back\ncreate error\ngl.fotechwealth.com.local/backend/trade-lib.git/dbaccess.(*accessor).Create\n\tD:/Users/yingjie.huang/go/pkg/mod/gl.fotechwealth.com.local/backend/trade-lib.git/dbaccess@v0.0.0-20221103065121-71047491dbb3/accessor.go:234\ntrade/internal/admin/service.(*wealthAdminService).CounterfoilBuyV2.func1\n\tD:/Work/wealth/internal/admin/service/order_create_counterfoil_v2.go:121\ngorm.io/gorm.(*DB).Transaction\n\tD:/Users/yingjie.huang/go/pkg/mod/gorm.io/gorm@v1.24.5/finisher_api.go:647\ntrade/internal/admin/service.(*wealthAdminService).CounterfoilBuyV2\n\tD:/Work/wealth/internal/admin/service/order_create_counterfoil_v2.go:106\ntrade/internal/admin/controller.(*wealthAdminController).CounterfoilBuyV2\n\tD:/Work/wealth/internal/admin/controller/admin_v2.go:77\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ntrade/middleware/ucuserinfo.Injector.func1\n\tD:/Work/wealth/middleware/ucuserinfo/middleware.go:87\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ntrade/middleware/auth.NewAdminPermission.func1\n\tD:/Work/wealth/middleware/auth/auth.go:422\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ngl.fotechwealth.com.local/backend/trade-lib.git/fiber/middleware/validator.Validator.func1\n\tD:/Users/yingjie.huang/go/pkg/mod/gl.fotechwealth.com.local/backend/trade-lib.git/fiber@v0.0.0-20240124010927-1532ceef9c99/middleware/validator/vaildator.go:38\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ngl.fotechwealth.com.local/backend/trade-lib.git/fiber/middleware/bodyparser.BodyParser.func1\n\tD:/Users/yingjie.huang/go/pkg/mod/gl.fotechwealth.com.local/backend/trade-lib.git/fiber@v0.0.0-20240124010927-1532ceef9c99/middleware/bodyparser/bodyparser.go:22\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ngl.fotechwealth.com.local/backend/trade-lib.git/fiber/middleware/requestparams.RequestParams.func1\n\tD:/Users/yingjie.huang/go/pkg/mod/gl.fotechwealth.com.local/backend/trade-lib.git/fiber@v0.0.0-20240124010927-1532ceef9c99/middleware/requestparams/request_params.go:24\ngithub.com/gofiber/fiber/v2.(*App).next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/router.go:145\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:970\ngl.fotechwealth.com.local/backend/trade-lib.git/apm.New.func1\n\tD:/Users/yingjie.huang/go/pkg/mod/gl.fotechwealth.com.local/backend/trade-lib.git/apm@v0.0.0-20230303093242-a29ea1a36732/apm.go:86\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ngl.fotechwealth.com.local/backend/trade-lib.git/fiber/middleware/trace.Trace.func1\n\tD:/Users/yingjie.huang/go/pkg/mod/gl.fotechwealth.com.local/backend/trade-lib.git/fiber@v0.0.0-20240124010927-1532ceef9c99/middleware/trace/trace.go:120\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ntrade/middleware/requestid.New.func1\n\tD:/Work/wealth/middleware/requestid/requestid.go:51\ngithub.com/gofiber/fiber/v2.(*Ctx).Next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/ctx.go:967\ntrade/middleware/recover.New.func1\n\tD:/Work/wealth/middleware/recover/recover.go:42\ngithub.com/gofiber/fiber/v2.(*App).next\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/router.go:145\ngithub.com/gofiber/fiber/v2.(*App).handler\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/gofiber/fiber/v2@v2.49.2/router.go:172\ngithub.com/valyala/fasthttp.(*Server).serveConn\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/valyala/fasthttp@v1.49.0/server.go:2357\ngithub.com/valyala/fasthttp.(*workerPool).workerFunc\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/valyala/fasthttp@v1.49.0/workerpool.go:224\ngithub.com/valyala/fasthttp.(*workerPool).getCh.func1\n\tD:/Users/yingjie.huang/go/pkg/mod/github.com/valyala/fasthttp@v1.49.0/workerpool.go:196\nruntime.goexit\n\tD:/go/src/runtime/asm_amd64.s:1594"
}
原本的代码:
go err = mysql.WithContext(ctx, s.dbAccess.DB()).Transaction(func(tx *gorm.DB) error {
// 生成买入订单并持久化到数据库中
order, err = s.noteSubscribeMakeOrderV2(ctx, sctx, tx, r, matchResult)
if err != nil {
return err
}
// 【票据订单中台增加对应销售信息】
// 创建跟进人信息
// 这里不明白为什么是取第一个,参考的现金宝
if len(assignDetails.List) > 0 {
assignStaffs := assignDetails.List[0].AssignStaffs
relationType := assignDetails.List[0].Relation
for _, staff := range assignStaffs {
err := s.UserOrderFollowUpDao.Create(ctx, tx, commontradedbmodel.TUserOrderFollowUp{
OrderId: order.Id,
Position: staff.Position,
RoleId: staff.RoleId,
RoleName: staff.RoleName,
StaffId: staff.StaffId,
StaffName: staff.StaffName,
StaffEmail: staff.StaffEmail,
RelationType: relationType,
})
if err != nil {
l.Error("create TUserOrderFollowUp failed", zap.Error(err))
return code.NewDefaultError(code.ErrDatabase)
}
}
}
return nil
})
新版本的代码:
go // 生成买入订单并持久化到数据库中
order, err = s.noteSubscribeMakeOrderV2(ctx, sctx, nil, r, matchResult)
if err != nil {
return nil, code.NewDefaultError(code.ErrDatabase)
}
// 【票据订单中台增加对应销售信息】
// 创建跟进人信息
// 这里不明白为什么是取第一个,参考的现金宝
if len(assignDetails.List) > 0 {
assignStaffs := assignDetails.List[0].AssignStaffs
relationType := assignDetails.List[0].Relation
for _, staff := range assignStaffs {
err := s.UserOrderFollowUpDao.Create(ctx, nil, &commontradedbmodel.TUserOrderFollowUp{
OrderId: order.Id,
Position: staff.Position,
RoleId: staff.RoleId,
RoleName: staff.RoleName,
StaffId: staff.StaffId,
StaffName: staff.StaffName,
StaffEmail: staff.StaffEmail,
RelationType: relationType,
})
if err != nil {
l.Error("create TUserOrderFollowUp failed", zap.Error(err))
return nil, code.NewDefaultError(code.ErrDatabase)
}
}
}
本文作者:JIeJaitt
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!