Ensure rollback when commit has error.
This commit is contained in:
parent
2821ef6da5
commit
a10fe4228c
9
main.go
9
main.go
@ -526,7 +526,7 @@ func (s *DB) Debug() *DB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Transaction start a transaction as a block,
|
// Transaction start a transaction as a block,
|
||||||
// return error will rollback, otherwise to commit
|
// return error will rollback, otherwise to commit.
|
||||||
func (s *DB) Transaction(fc func(tx *DB) error) (err error) {
|
func (s *DB) Transaction(fc func(tx *DB) error) (err error) {
|
||||||
tx := s.Begin()
|
tx := s.Begin()
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -539,10 +539,13 @@ func (s *DB) Transaction(fc func(tx *DB) error) (err error) {
|
|||||||
|
|
||||||
err = fc(tx)
|
err = fc(tx)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
err = tx.Commit().Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Makesure rollback when Block error or Commit error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
} else {
|
|
||||||
err = tx.Commit().Error
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user