From b73221cebf7580425d03dd90faed2d7e4fd9dee6 Mon Sep 17 00:00:00 2001 From: Hazel Wright Date: Wed, 5 Aug 2020 10:21:26 +0100 Subject: [PATCH] docs[django]: transaction.atomic behaviour --- languages/Python/index.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/languages/Python/index.md b/languages/Python/index.md index 19d89f7..3841b5e 100644 --- a/languages/Python/index.md +++ b/languages/Python/index.md @@ -132,6 +132,8 @@ Models, Enum classes and apps should have singular names. Avoid `select_related` with no parameters. It's performance characteristics can change wildly as the data model evolves. +Transactions should be as short as possible, as open transactions have a performance cost for the database and we can only maintain a certain number of open transactions at any one time. This means that you should prefer using `transaction.atomic` as a context manager (`with transaction.atomic()`) over as a decorator (`@transaction.atomic`), unless your function only does database access calls and you genuinely need transactional behaviour throughout. Avoid calling external services inside a transaction, as the round trip to and from the external service will cause your transaction to stay open for an artificially long time. + Where the standard Django app modules `models.py`, `forms.py`, `admin.py`, etc get too large to be workable, convert into a package containing multiple modules.