Preambulo:
Django tries to support as many resources as possible in all database backends. However, not all backends are equal and the solution was to make design decisions about what features should be supported and what assumptions developers (from Django) can safely make.
Persistent connections:
Persistent connections avoid the overhead of reestablishing a database connection at each request. They are controlled by the parameter CONN_MAX_AGE
, setting the maximum duration of a connection. It can be set independently for each database.
The default value is 0, preserving the historical behavior of closing the connection to the database at the end of each request. To enable persistent connections, configure CONN_MAX_AGE
to a positive number (in seconds). For unlimited persistent connections, set it to None
.
Connection management:
Django opens a connection when it queries the database. It keeps that connection open and reuses it in subsequent requests. Django closes the connection once it exceeds the maximum time set by CONN_MAX_AGE
or when it can no longer be used.
In detail, Django automatically opens a connection to the database whenever it needs one and no longer has one - either because this is the first connection, or because the previous connection was closed.
At the beginning of each request, Django closes the connection if it reaches its "maximum age". If your database shuts down inactive connections after some time, you should set CONN_MAX_AGE
for a lower value, so that Django does not attempt to use a connection that has been terminated by the database server. (This problem can affect only very low traffic websites.)
At the end of each request, Django closes the connection if it reaches its "maximum age" or if it is in an unrecoverable error state. If a database error occurred while processing the requests, Django checks whether the connection still works, and closes if it doesn’t work. Thus, database errors affect at most one request; if the connection becomes unusable, the next request will receive a new connection.
Caveats:
As each thread maintains its own connection, your database should support at least as many simultaneous connections as the number of threaded threads.
Sometimes a database is not accessed by most of its views, for example because it is an external system database or because of caching. In such cases, you must define CONN_MAX_AGE
as a low value or up to 0, as it makes no sense to maintain a connection that will probably not be reused. This will help maintain a small number of simultaneous connections to this database.
The development server creates a new thread for each request, denying the effect of persistent connections. Do not enable it during development.
When Django establishes a connection to the database, it configures appropriate parameters depending on the backend being used. If you enable persistent connections, this setting will no longer be repeated in all requests. If you modify parameters, such as isolation level
or the time zone
, should restore Django defaults at the end of each request, force an appropriate value at the beginning of each request, or disable persistent connections.
Reference:
https://docs.djangoproject.com/en/2.2/ref/databases/