From 6e2cfa5b933a16a3a238471d3b83f33337f2bbc2 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 11 Nov 2022 13:48:24 +0800 Subject: [PATCH] extract function --- models/db/context.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/models/db/context.go b/models/db/context.go index b5d029c045d5..1bca284e504f 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -110,18 +110,7 @@ func WithTx(parentCtx context.Context, f func(ctx context.Context) error) error if InTransaction(parentCtx) { return ErrAlreadyInTransaction } - - sess := x.NewSession() - defer sess.Close() - if err := sess.Begin(); err != nil { - return err - } - - if err := f(newContext(parentCtx, sess, true)); err != nil { - return err - } - - return sess.Commit() + return txWithNoCheck(parentCtx, f) } // AutoTx represents executing database operations on a transaction, if the transaction exist, @@ -130,7 +119,10 @@ func AutoTx(parentCtx context.Context, f func(ctx context.Context) error) error if InTransaction(parentCtx) { return f(newContext(parentCtx, GetEngine(parentCtx), true)) } + return txWithNoCheck(parentCtx, f) +} +func txWithNoCheck(parentCtx context.Context, f func(ctx context.Context) error) error { sess := x.NewSession() defer sess.Close() if err := sess.Begin(); err != nil {