From d6f59dc83a9d784febcecfe6dd8fdbd0971826b6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simon=20K=C3=BCnzel?= <simonk@fsmpi.rwth-aachen.de>
Date: Sat, 3 May 2025 20:48:15 +0200
Subject: [PATCH] Improve database conflict logging

---
 common_py/src/videoag_common/database/database.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/common_py/src/videoag_common/database/database.py b/common_py/src/videoag_common/database/database.py
index 079c9b7..a306e64 100644
--- a/common_py/src/videoag_common/database/database.py
+++ b/common_py/src/videoag_common/database/database.py
@@ -201,6 +201,8 @@ class Database:
                         transaction.rollback()
                     except Exception:
                         pass
+                if attempts > 1:
+                    print(f"Reattempt of transaction which had conflict was successful after {attempts} attempts")
                 return result
             except Exception as e:
                 is_conflict = Database._is_transaction_conflict_exception(e)
@@ -220,12 +222,13 @@ class Database:
                 self._on_transaction_conflict(writeable)
                 if attempts >= (self._max_write_attempts if writeable else self._max_read_attempts):
                     self._on_transaction_aborted_after_repeated_conflict(writeable)
+                    print(f"Warning: Transaction failed due to conflict after {attempts} attempts", file=sys.stderr)
                     raise TransactionConflictError(e)
                 
                 # Sleep a random time for up to 500ms to increase chance that next attempt succeeds (The randomness makes
                 # it more likely that we reattempt at a different time than the one with which we are in conflict right now)
                 time.sleep(random.random() / 2)
-                print("Warning: Reattempting transaction after commit conflict", file=sys.stderr)
+                print("Warning: Reattempting transaction after commit conflict:", file=sys.stderr)
                 traceback.print_exception(e)
                 continue
                 
-- 
GitLab