From d69cd8706ca7b205b55bb77ee123f5c2b4abc061 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Mon, 25 Mar 2024 13:59:38 +0800 Subject: [PATCH 1/2] fix: commit trasaction --- models/issues/review.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/issues/review.go b/models/issues/review.go index fc110630e0fa8..68de2d124f903 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, From da585527d962ffd2fd80fab200f5843ebd90f00d Mon Sep 17 00:00:00 2001 From: Jason Song Date: Mon, 25 Mar 2024 14:25:38 +0800 Subject: [PATCH 2/2] chore: add comments --- models/db/context.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/models/db/context.go b/models/db/context.go index cda608af19669..43f612518aacf 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