You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 11 Next »

gdb事务操作比较简单,可以通过两种操作方式来实现。

  1. 一种是开启事务之后会返回一个事务操作对象*gdb.TX,随后可以使用该对象进行如之前章节介绍的方法操作和链式操作。
  2. 一种是以闭包的形式来操作事务,所有的事务逻辑在闭包中实现。

接口文档: https://godoc.org/github.com/gogf/gf/database/gdb#TX

Begin/Commit/Rollback

开启事务操作可以通过执行db.Begin方法,该方法返回事务的操作对象,类型为*gdb.Tx,通过该对象执行后续的数据库操作,并可通过tx.Commit提交修改,或者通过tx.Rollback回滚修改。

常见问题注意:开启事务操作后,请务必在不需要使用该事务对象时,通过Commit/Rollback操作关闭掉该事务,建议充分利用好defer方法。如果事务使用后不关闭,在应用侧会引起goroutine不断激增泄露,在数据库侧会引起事务线程数量被打满,以至于后续的事务请求执行超时。此外,建议尽可能使用后续介绍的Transaction闭包方法来安全实现事务操作。

1. 开启事务操作

if tx, err := db.Begin(); err == nil {
    fmt.Println("开启事务操作")
}

事务操作对象可以执行所有db对象的方法,具体请参考 API文档

2. 事务回滚操作

if tx, err := db.Begin(); err == nil {
    r, err := tx.Save("user", gdb.Map{
        "uid"  :  1,
        "name" : "john",
    })
    tx.Rollback()
    fmt.Println(r, err)
}

3. 事务提交操作

if tx, err := db.Begin(); err == nil {
    r, err := tx.Save("user", gdb.Map{
        "uid"  :  1,
        "name" : "john",
    })
    tx.Commit()
    fmt.Println(r, err)
}

4. 事务链式操作

事务操作对象仍然可以通过tx.Table或者tx.From方法返回一个链式操作的对象,该对象与db.Table或者db.From方法返回值相同,只不过数据库操作在事务上执行,可提交或回滚。

if tx, err := db.Begin(); err == nil {
    r, err := tx.Table("user").Data(gdb.Map{"uid":1, "name": "john_1"}).Save()
    tx.Commit()
    fmt.Println(r, err)
}

其他链式操作请参考 ORM链式操作 章节。

Transaction闭包操作

为方便安全执行事务操作,gdb提供了事务的闭包操作,通过Transaction方法实现,该方法定义如下:

func (db DB) Transaction(f func(tx *TX) error) (err error)

当给定的闭包方法返回的errornil时,那么闭包执行结束后当前事务自动执行Commit提交操作;否则自动执行Rollback回滚操作。

如果闭包内部操作产生panic中断,该事务也将进行回滚。

使用示例:

db.Transaction(func(tx *gdb.TX) error {
    // user
    result, err := tx.Insert("user", g.Map{
        "passport": "john",
        "password": "12345678",
        "nickname": "JohnGuo",
    })
    if err != nil {
        return err
    }
    // user_detail
    id, err := result.LastInsertId()
    if err != nil {
        return err
    }
    _, err = tx.Insert("user_detail", g.Map{
        "uid":       id,
        "site":      "https://johng.cn",
        "true_name": "GuoQiang",
    })
    if err != nil {
        return err
    }
    return nil
})

嵌套事务操作

GoFrame版本v1.15.7开始,提供了对数据库嵌套事务的支持。相关方法:

// Commit commits current transaction.
// Note that it releases previous saved transaction point if it's in a nested transaction procedure,
// or else it commits the hole transaction.
func (tx *TX) Commit() error

// Rollback aborts current transaction.
// Note that it aborts current transaction if it's in a nested transaction procedure,
// or else it aborts the hole transaction.
func (tx *TX) Rollback() error

// Begin starts a nested transaction procedure.
func (tx *TX) Begin() error

// SavePoint performs `SAVEPOINT xxx` SQL statement that saves transaction at current point.
// The parameter `point` specifies the point name that will be saved to server.
func (tx *TX) SavePoint(point string) error

// RollbackTo performs `ROLLBACK TO SAVEPOINT xxx` SQL statement that rollbacks to specified saved transaction.
// The parameter `point` specifies the point name that was saved previously.
func (tx *TX) RollbackTo(point string) error

// 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 (tx *TX) Transaction(f func(tx *TX) error) (err error)

Begin/Rollback/Commit

示例SQL:

CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL COMMENT '用户ID',
  `name` varchar(45) NOT NULL COMMENT '用户名称',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

示例程序代码:

tx, err := db.Begin()
if err != nil {
	panic(err)
}
if err = tx.Begin(); err != nil {
	panic(err)
}
_, err = tx.Model(table).Data(g.Map{"id": 1, "name": "john"}).Insert()
if err = tx.Rollback(); err != nil {
	panic(err)
}
_, err = tx.Model(table).Data(g.Map{"id": 2, "name": "smith"}).Insert()
if err = tx.Commit(); err != nil {
	panic(err)
}

执行后查询数据库结果:

可以看到第一个操作被回滚成功,只有第二个操作执行成。








Content Menu

  • No labels