I'm using MySQL with the InnoDB engine and REPEATABLE-READ isolation level.
I have written a function which I think should atomically increment an IntegerField, and give me the value after the increment. I would like opinions on whether my code is robust against concurrency issues.
My code looks like this:
class MyModel(models.Model):
version = models.IntegerField()
@staticmethod
@transaction.commit_on_success
def acquire_version(pk):
MyModel.objects.filter(pk = pk).update(version = F('version') + 1)
return MyModel.objects.get(pk = pk).version
My thinking is that in two concurrent calls, the UPDATEs will mutually exclude one another because of a write lock, and then REPEATABLE-READ should guarantee that my subsequent .get will give me the value after the UPDATE. Am I right?
(This is not a general "how do I do an atomic increment?" question, there's already one of those. This is about whether this one particular way is valid.)