Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: trans error rollback #6876

Closed
wants to merge 1 commit into from
Closed

Conversation

a631807682
Copy link
Member

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

close #6826

User Case Description

@jinzhu
Copy link
Member

jinzhu commented Mar 9, 2024

If a commit fails, is a rollback needed? Does it automatically rollback?

@iTanken
Copy link
Contributor

iTanken commented Mar 11, 2024

If a commit fails, is a rollback needed? Does it automatically rollback?

gorm/finisher_api.go

Lines 638 to 655 in e4e23d2

} else {
tx := db.Begin(opts...)
if tx.Error != nil {
return tx.Error
}
defer func() {
// Make sure to rollback when panic, Block error or Commit error
if panicked || err != nil {
tx.Rollback()
}
}()
if err = fc(tx); err == nil {
panicked = false
return tx.Commit().Error
}
}

// Make sure to rollback when panic, Block error or Commit error

@jinzhu
Copy link
Member

jinzhu commented Mar 11, 2024

// Make sure to rollback when panic, Block error or Commit error

We code for reality, not for comments ;)

After doing some research, I think that modern databases no longer require calling rollback after a failed commit. We might need to revise the comment above.

@a631807682
Copy link
Member Author

Commit are usually divided into writing commit packet and reading results. If writing fails, we not need to rollback. Failure of reading results usually comes from network errors, which means our rollback may not succeed.
This does not seem to be necessary, is there any actual case?

@a631807682 a631807682 closed this Mar 14, 2024
@a631807682 a631807682 deleted the fix_trans_err branch March 14, 2024 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rollback logic error in "func (db *DB) Transaction"
3 participants