summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGiteabot <teabot@gitea.io>2024-03-25 15:22:09 +0800
committerGitHub <noreply@github.com>2024-03-25 07:22:09 +0000
commit78795dd5663b7d8df4620bd50c74a7d71606f1d2 (patch)
treea5033c4e329b3f4c78cfce7ee7ed9a9f1d0b7664
parente321b8a849087d736a96275d5960f9b1446c95ba (diff)
downloadgitea-78795dd5663b7d8df4620bd50c74a7d71606f1d2.tar.gz
gitea-78795dd5663b7d8df4620bd50c74a7d71606f1d2.zip
Fix misuse of `TxContext` (#30061) (#30062)
Backport #30061 by @wolfogre Help #29999, or its tests cannot pass. Also, add some comments to clarify the usage of `TxContext`. I don't check all usages of `TxContext` because there are too many (almost 140+). It's a better idea to replace them with `WithTx` instead of checking them one by one. However, that may be another refactoring PR. Co-authored-by: Jason Song <i@wolfogre.com>
-rw-r--r--models/db/context.go10
-rw-r--r--models/issues/review.go2
2 files changed, 11 insertions, 1 deletions
diff --git a/models/db/context.go b/models/db/context.go
index 9f72b43555..fc1fecb0e3 100644
--- a/models/db/context.go
+++ b/models/db/context.go
@@ -120,6 +120,16 @@ func (c *halfCommitter) Close() error {
// TxContext represents a transaction Context,
// it will reuse the existing transaction in the parent context or create a new one.
+// Some tips to use:
+//
+// 1 It's always recommended to use `WithTx` in new code instead of `TxContext`, since `WithTx` will handle the transaction automatically.
+// 2. To maintain the old code which uses `TxContext`:
+// a. Always call `Close()` before returning regardless of whether `Commit()` has been called.
+// b. Always call `Commit()` before returning if there are no errors, even if the code did not change any data.
+// c. Remember the `Committer` will be a halfCommitter when a transaction is being reused.
+// So calling `Commit()` will do nothing, but calling `Close()` without calling `Commit()` will rollback the transaction.
+// And all operations submitted by the caller stack will be rollbacked as well, not only the operations in the current function.
+// d. It doesn't mean rollback is forbidden, but always do it only when there is an error, and you do want to rollback.
func TxContext(parentCtx context.Context) (*Context, Committer, error) {
if sess, ok := inTransaction(parentCtx); ok {
return newContext(parentCtx, sess, true), &halfCommitter{committer: sess}, nil
diff --git a/models/issues/review.go b/models/issues/review.go
index 718708ec59..c70bdc643b 100644
--- a/models/issues/review.go
+++ b/models/issues/review.go
@@ -621,7 +621,7 @@ func AddReviewRequest(ctx context.Context, issue *Issue, reviewer, doer *user_mo
// skip it when reviewer hase been request to review
if review != nil && review.Type == ReviewTypeRequest {
- return nil, nil
+ return nil, committer.Commit() // still commit the transaction, or committer.Close() will rollback it, even if it's a reused transaction.
}
// if the reviewer is an official reviewer,