Skip to content

Commit

Permalink
prevent clashing locks: don't lock entire table
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Jan 5, 2024
1 parent 33d1b6c commit 2399fd7
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions hordak/receivers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.db import connection
from django.db import transaction
from django.db.models import F
from django.db.models.signals import post_delete, pre_save
from django.dispatch import receiver
Expand All @@ -21,10 +21,9 @@ def update_running_totals(sender, instance, **kwargs):
amount_change = instance.amount

amount_change = instance.account.sign * amount_change
with connection.cursor() as cursor:
cursor.execute("LOCK TABLE %s IN EXCLUSIVE MODE" % RunningTotal._meta.db_table)

running_total, created = RunningTotal.objects.get_or_create(
with transaction.atomic():
# Lock the specific running total row
running_total, created = RunningTotal.objects.select_for_update().get_or_create(
account=instance.account,
currency=instance.amount.currency,
defaults={"balance": amount_change},
Expand Down

0 comments on commit 2399fd7

Please sign in to comment.