Skip to content

Commit 3d59235

Browse files
committed
prevent clashing locks: don't lock entire table
1 parent d88a5b4 commit 3d59235

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

hordak/receivers.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django.db import connection
1+
from django.db import transaction
22
from django.db.models import F
33
from django.db.models.signals import post_delete, pre_save
44
from django.dispatch import receiver
@@ -21,10 +21,9 @@ def update_running_totals(sender, instance, **kwargs):
2121
amount_change = instance.amount
2222

2323
amount_change = instance.account.sign * amount_change
24-
with connection.cursor() as cursor:
25-
cursor.execute("LOCK TABLE %s IN EXCLUSIVE MODE" % RunningTotal._meta.db_table)
26-
27-
running_total, created = RunningTotal.objects.get_or_create(
24+
with transaction.atomic():
25+
# Lock the specific running total row
26+
running_total, created = RunningTotal.objects.select_for_update().get_or_create(
2827
account=instance.account,
2928
currency=instance.amount.currency,
3029
defaults={"balance": amount_change},

0 commit comments

Comments
 (0)