Skip to content

Commit

Permalink
extract function
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Nov 11, 2022
1 parent 1eb7d93 commit 6e2cfa5
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions models/db/context.go
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 6e2cfa5

Please sign in to comment.