Kit Sunde
Kit Sunde

Reputation: 37075

Does select_for_update release the lock on first save() or at the end of the view?

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?

Upvotes: 9

Views: 4623

Answers (1)

Kit Sunde
Kit Sunde

Reputation: 37075

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.

Upvotes: 9

Related Questions