diff --git a/internal/dao/pgx/account_pgx.go b/internal/dao/pgx/account_pgx.go index 376a5f3e..9cb07c4e 100644 --- a/internal/dao/pgx/account_pgx.go +++ b/internal/dao/pgx/account_pgx.go @@ -9,6 +9,7 @@ import ( "github.com/RHEnVision/provisioning-backend/internal/db" "github.com/RHEnVision/provisioning-backend/internal/models" "github.com/georgysavva/scany/v2/pgxscan" + "github.com/rs/zerolog" ) func init() { @@ -76,6 +77,15 @@ func (x *accountDao) GetOrCreateByIdentity(ctx context.Context, orgId string, ac return nil, fmt.Errorf("pgx error: %w", err) } + // Step 4: requery in case transaction isolation (simultaneous transactions) + if result.ID == 0 && result.OrgID == "" { + zerolog.Ctx(ctx).Warn().Bool("requery", true).Msgf("Organization id %s requery", orgId) + err = pgxscan.Get(ctx, db.Pool, result, query, orgId, account) + if err != nil { + return nil, fmt.Errorf("pgx requery error: %w", err) + } + } + return result, nil } diff --git a/internal/middleware/account.go b/internal/middleware/account.go index ce59fd2c..71f93284 100644 --- a/internal/middleware/account.go +++ b/internal/middleware/account.go @@ -44,11 +44,10 @@ func AccountMiddleware(next http.Handler) http.Handler { logger.Error().Err(err).Msg("Cache returned error") http.Error(w, err.Error(), 500) return + } else { + logger.Trace().Int64("account", cachedAccount.ID).Msg("Account cache hit") } - // account found in cache - logger.Trace().Int64("account", cachedAccount.ID).Msg("Account cache hit") - // set contexts - account id ctx := identity.WithAccountId(r.Context(), cachedAccount.ID)