I have a function fn()
, which should atomize working with a database, which relies on a certain set of data that does not change during its execution (though most of the time).
What is the correct way to implement this in Django? Basically I would like to do something like this:
def ensure_fn_runs_successfully():
@transaction.atomic
takes care of part of the problem (the database should only see the state before fn
runs or after fn
runs successfully), but I'm not sure if there is a good primitive to execute commit_only_if_the_data_actually_didnt_change
and retry the operation if it fails.
To verify that the data has not changed, just check that the number of returned items for the request is the same as at the beginning of the function; however, I do not know if there are any primitives that allow you to check and make decisions at the same time / without race conditions.
source share