事务

原文:https://beego.wiki/docs/mvc/model/transaction/

Transaction 事务

Transaction 事务

There are two ways to handle transaction in Beego. One is closure:

​ 在 Beego 中有两种处理事务的方法。一种是闭包:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Beego will manage the transaction's lifecycle
// if the @param task return error, the transaction will be rollback
// or the transaction will be committed
err := o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
	// data
	user := new(User)
	user.Name = "test_transaction"

	// insert data
	// Using txOrm to execute SQL
	_, e := txOrm.Insert(user)
	// if e != nil the transaction will be rollback
	// or it will be committed
	return e
})

In this way, the first parameter is task, all DB operation should be inside the task.

​ 这种方式中,第一个参数是 task ,所有数据库操作都应该在任务内部。

If the task return error, Beego rollback the transaction.

​ 如果任务返回错误,Beego 会回滚事务。

We recommend you to use this way.

​ 我们建议您使用这种方式。

Another way is that users handle transaction manually:

​ 另一种方式是用户手动处理事务:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
	o := orm.NewOrm()
	to, err := o.Begin()
	if err != nil {
		logs.Error("start the transaction failed")
		return
	}

	user := new(User)
	user.Name = "test_transaction"

	// do something with to. to is an instance of TxOrm

	// insert data
	// Using txOrm to execute SQL
	_, err = to.Insert(user)

	if err != nil {
		logs.Error("execute transaction's sql fail, rollback.", err)
		err = to.Rollback()
		if err != nil {
			logs.Error("roll back transaction failed", err)
		}
	} else {
		err = to.Commit()
		if err != nil {
			logs.Error("commit transaction failed.", err)
		}
	}
o := orm.NewOrm()
to, err := o.Begin()

// outside the txn
o.Insert(xxx)

// inside the txn
to.Insert(xxx)
最后修改 October 10, 2024: 更新 (a4b8f85)