I have something like:
newsletter = Newsletter.select_for_update().latest()
newsletter.started_sending = timezone.now()
newsletter.save()
newsletter.send()
Then it hit a race condition where I was sending duplicates, presumably because send() was being hit by two cron jobs. Which leads me to think that the lock is released on first save, where as I thought it wouldn't release until the end of the view.
It's released on first save, right?
From https://docs.djangoproject.com/en/dev/topics/db/transactions/#topics-db-transactions-requirements
Django’s default transaction behavior
Django’s default behavior is to run with an open transaction which it commits automatically when any built-in, data-altering model function is called. For example, if you call model.save() or model.delete(), the change will be committed immediately.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With