A common way to handle transactions on the web is to wrap each request in a transaction. In Django, you can set ATOMIC_REQUESTS
to True in the configuration of each database for which you want to enable this behavior.
It works like this. Before calling a view function, Django starts a transaction. If the response is produced without problems, Django commits the transaction. If the view produces an exception, Django rolls back the transaction.
While the simplicity of this transaction model is appealing, it also makes it inefficient when traffic increases. Opening a transaction for every view has some overhead.
For requests that do not need to be wrapped in transactions, you can apply the @transaction.non_atomic_requests
decorator.
Given a Django view.py
with several View classes and request methods. How might I go about deciding which requests would make good candidates for the non-atomic decorator?
What "gotchas" might be lurking?
I could see POST
methods not making good candidates or anything with a .save()
, but what else should I look out for?