Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are F() expressions and atomic transactions mutually replaceable?

Is it safe to replace this code:

def view(request):
    reporter.stories_filed = F('stories_filed') + 1

with this:

@transaction.atomic
def view(request):
    reporter.stories_filed += 1       

and is this wrong:

@transaction.atomic
def view(request):
    reporter.stories_filed = F('stories_filed') + 1

?

like image 217
Bob Avatar asked Oct 30 '22 22:10

Bob


1 Answers

The answer depends on the transaction isolation level used (most often the answer is NO)

I found a very detailed explanation here:

https://en.wikipedia.org/wiki/Isolation_(database_systems)

A brief summary: There are several transaction isolation levels which affect how transactions function, which level is used by Django depends on your RDBMS, it's settings etc.

Only "Serializable" transaction isolation level will prevent race conditions (the purpose of F object). And it is not used by default in most RDBMS and Django.

And the purpose of transactions is mostly another thing: to ensure that either all or none statements inside a transaction are committed.

like image 134
Bob Avatar answered Nov 11 '22 13:11

Bob