github.com/wangyougui/gf/v2@v2.6.5/database/gdb/gdb_model_transaction.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gdb 8 9 import ( 10 "context" 11 ) 12 13 // Transaction wraps the transaction logic using function `f`. 14 // It rollbacks the transaction and returns the error from function `f` if 15 // it returns non-nil error. It commits the transaction and returns nil if 16 // function `f` returns nil. 17 // 18 // Note that, you should not Commit or Rollback the transaction in function `f` 19 // as it is automatically handled by this function. 20 func (m *Model) Transaction(ctx context.Context, f func(ctx context.Context, tx TX) error) (err error) { 21 if ctx == nil { 22 ctx = m.GetCtx() 23 } 24 if m.tx != nil { 25 return m.tx.Transaction(ctx, f) 26 } 27 return m.db.Transaction(ctx, f) 28 }