Skip to main content

Transaction Handling

Conventional Approach

tx, err := g.DB().Begin(ctx)

if err == nil {
_, err := tx.Model("book").Data(data).Save()
if err == nil {
tx.Commit()
} else {
tx.Rollback()
}
}

Closure Approach (Recommended by the Framework)

g.DB().Transaction(context.TODO(), func(ctx context.Context, tx gdb.TX) error {
_, err := tx.Model("book").Ctx(ctx).Save(data)
return err
})