From 3d3264eb8f1420b590219dab2f4d8791302d28ae Mon Sep 17 00:00:00 2001 From: Stefan Benz <46600784+stebenz@users.noreply.github.com> Date: Thu, 11 Jan 2024 00:02:50 +0100 Subject: [PATCH] fix: add RollbackUnlessCommitted for gorm transactions (#7197) --- internal/view/repository/query.go | 1 + internal/view/repository/requests.go | 3 +++ 2 files changed, 4 insertions(+) diff --git a/internal/view/repository/query.go b/internal/view/repository/query.go index 4b931b7d5e..b3be1f0163 100644 --- a/internal/view/repository/query.go +++ b/internal/view/repository/query.go @@ -57,6 +57,7 @@ func PrepareSearchQuery(table string, request SearchRequest) func(db *gorm.DB, r if err := query.Commit().Error; err != nil { logging.OnError(err).Info("commit failed") } + query.RollbackUnlessCommitted() }() query = query.Count(&count) diff --git a/internal/view/repository/requests.go b/internal/view/repository/requests.go index b1d98af34f..54b4e79c3f 100644 --- a/internal/view/repository/requests.go +++ b/internal/view/repository/requests.go @@ -9,6 +9,7 @@ import ( "github.com/jinzhu/gorm" "github.com/zitadel/logging" + "github.com/zitadel/zitadel/internal/zerrors" ) @@ -28,6 +29,7 @@ func PrepareGetByQuery(table string, queries ...SearchQuery) func(db *gorm.DB, r if err := tx.Commit().Error; err != nil { logging.OnError(err).Info("commit failed") } + tx.RollbackUnlessCommitted() }() err := tx.Take(res).Error @@ -46,6 +48,7 @@ func PrepareBulkSave(table string) func(db *gorm.DB, objects ...interface{}) err return func(db *gorm.DB, objects ...interface{}) error { db = db.Table(table) db = db.Begin() + defer db.RollbackUnlessCommitted() if err := db.Error; err != nil { return zerrors.ThrowInternal(err, "REPOS-Fl0Is", "unable to begin") }