mirror of
https://gitee.com/johng/gf
synced 2026-07-03 03:39:35 +08:00
add function Transaction for gdb.Model
This commit is contained in:
28
database/gdb/gdb_model_transaction.go
Normal file
28
database/gdb/gdb_model_transaction.go
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// Transaction wraps the transaction logic using function `f`.
|
||||
// It rollbacks the transaction and returns the error from function `f` if
|
||||
// it returns non-nil error. It commits the transaction and returns nil if
|
||||
// function `f` returns nil.
|
||||
//
|
||||
// Note that, you should not Commit or Rollback the transaction in function `f`
|
||||
// as it is automatically handled by this function.
|
||||
func (m *Model) Transaction(ctx context.Context, f func(ctx context.Context, tx *TX) error) (err error) {
|
||||
if ctx == nil {
|
||||
ctx = m.GetCtx()
|
||||
}
|
||||
if m.tx != nil {
|
||||
return m.tx.Transaction(ctx, f)
|
||||
}
|
||||
return m.db.Transaction(ctx, f)
|
||||
}
|
||||
Reference in New Issue
Block a user