pythondjangopostgresqltransactionsacid

Not nesting version of @atomic() in Django?


From the docs of atomic()

atomic blocks can be nested

This sound like a great feature, but in my use case I want the opposite: I want the transaction to be durable as soon as the block decorated with @atomic() gets left successfully.

Is there a way to ensure durability in django's transaction handling?

Background

Transaction are ACID. The "D" stands for durability. That's why I think transactions can't be nested without loosing feature "D".

Example: If the inner transaction is successful, but the outer transaction is not, then the outer and the inner transaction get rolled back. The result: The inner transaction was not durable.

I use PostgreSQL, but AFAIK this should not matter much.


Solution

  • Even though this exact behaviour is not possible, since django 3.2 there is a durable=True[@transaction.atomic(durable=True)] option to make sure that such a block of code isnt nested, so that by chance if such code is run as nested it results in a RuntimeError error. https://docs.djangoproject.com/en/dev/topics/db/transactions/#django.db.transaction.atomic An article on this issue https://seddonym.me/2020/11/19/trouble-atomic/