@@ -67,22 +67,18 @@ Some actions should allow for rollback when database record insertion/update/del
6767So services must be allowed to create a database transaction. Here is some example,
6868
6969``` go
70- // servcies/repository/repo.go
71- func CreateXXXX () error {\
72- ctx , committer , err := db.TxContext ()
73- if err != nil {
74- return err
75- }
76- defer committer.Close ()
77-
78- // do something, if return err, it will rollback automatically when `committer.Close()` is invoked.
79- if err := issues.UpdateIssue (ctx, repoID); err != nil {
80- // ...
81- }
82-
83- // ......
84-
85- return committer.Commit ()
70+ // services/repository/repository.go
71+ func CreateXXXX () error {
72+ return db.WithTx (func (ctx context.Context ) error {
73+ e := db.GetEngine (ctx)
74+ // do something, if err is returned, it will rollback automatically
75+ if err := issues.UpdateIssue (ctx, repoID); err != nil {
76+ // ...
77+ return err
78+ }
79+ // ...
80+ return nil
81+ })
8682}
8783```
8884
@@ -94,14 +90,14 @@ If the function will be used in the transaction, just let `context.Context` as t
9490func UpdateIssue (ctx context .Context , repoID int64 ) error {
9591 e := db.GetEngine (ctx)
9692
97- // ......
93+ // ...
9894}
9995```
10096
10197### Package Name
10298
10399For the top level package, use a plural as package name, i.e. ` services ` , ` models ` , for sub packages, use singular,
104- i.e. ` servcies /user` , ` models/repository ` .
100+ i.e. ` services /user` , ` models/repository ` .
105101
106102### Import Alias
107103
0 commit comments