Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

django-orm

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?

like image 700
Kit Sunde Avatar asked Aug 29 '12 07:08

Kit Sunde


Video Answer


1 Answers

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.

like image 105
Kit Sunde Avatar answered Sep 19 '22 20:09

Kit Sunde