Skip to content

Commit

Permalink
Don't AddError for Rollback on ErrTxDone (go-gorm#2434)
Browse files Browse the repository at this point in the history
  • Loading branch information
j16r authored and jinzhu committed Jun 11, 2019
1 parent 9127f7d commit 280dd01
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,9 @@ func (s *DB) Commit() *DB {
func (s *DB) Rollback() *DB {
var emptySQLTx *sql.Tx
if db, ok := s.db.(sqlTx); ok && db != nil && db != emptySQLTx {
s.AddError(db.Rollback())
if err := db.Rollback(); err != nil && err != sql.ErrTxDone {
s.AddError(err)
}
} else {
s.AddError(ErrInvalidTransaction)
}
Expand Down
16 changes: 16 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,22 @@ func TestTransaction(t *testing.T) {
}
}

func TestTransaction_NoErrorOnRollbackAfterCommit(t *testing.T) {
tx := DB.Begin()
u := User{Name: "transcation"}
if err := tx.Save(&u).Error; err != nil {
t.Errorf("No error should raise")
}

if err := tx.Commit().Error; err != nil {
t.Errorf("Commit should not raise error")
}

if err := tx.Rollback().Error; err != nil {
t.Errorf("Rollback should not raise error")
}
}

func TestRow(t *testing.T) {
user1 := User{Name: "RowUser1", Age: 1, Birthday: parseTime("2000-1-1")}
user2 := User{Name: "RowUser2", Age: 10, Birthday: parseTime("2010-1-1")}
Expand Down

0 comments on commit 280dd01

Please sign in to comment.