fix: add RollbackUnlessCommitted for gorm transactions (#7197)

This commit is contained in:
Stefan Benz 2024-01-11 00:02:50 +01:00 committed by GitHub
parent 7c592ce638
commit 3d3264eb8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 0 deletions

View File

@ -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)

View File

@ -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")
}