Persistent connections CONN_MAX_AGE django
Persistent connections¶
Persistent connections avoid the overhead of re-establishing a connection to the database in each request. They’re controlled by theCONN_MAX_AGE
parameter which defines the maximum lifetime of a connection. It can be set independently for each database.
The default value is 0
, preserving the historical behavior of closing the database connection at the end of each request. To enable persistent connections, set CONN_MAX_AGE
to a positive number of seconds. For unlimited persistent connections, set it to None
.
Connection management¶
Django opens a connection to the database when it first makes a database query. It keeps this connection open and reuses it in subsequent requests. Django closes the connection once it exceeds the maximum age defined by CONN_MAX_AGE
or when it isn’t usable any longer.
In detail, Django automatically opens a connection to the database whenever it needs one and doesn’t have one already — 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 has reached its maximum age. If your database terminates idle connections after some time, you should set CONN_MAX_AGE
to a lower value, so that Django doesn’t attempt to use a connection that has been terminated by the database server. (This problem may only affect very low traffic sites.)
At the end of each request, Django closes the connection if it has reached its maximum age or if it is in an unrecoverable error state. If any database errors have occurred while processing the requests, Django checks whether the connection still works, and closes it if it doesn’t. Thus, database errors affect at most one request; if the connection becomes unusable, the next request gets a fresh connection.
相关文章
- Django 使用django-celery-beat实现动态添加周期性任务
- django unique_together 引发内部错误
- django笔记整理
- Django之数据聚合函数 annotate
- 第三百八十三节,Django+Xadmin打造上线标准的在线教育平台—第三方模块django-simple-captcha验证码
- 基于Django的Disqus如何支持每月80亿PV(转)
- Django中如何使用django-celery完成异步任务2(转)
- Django ORM之QuerySet
- django框架中的全文检索Haystack
- 【转】django的ORM操作数据库样例
- 将Python的Django框架与认证系统整合的方法
- Python Django后台管理模板美化:使用django-simpleui模块
- Django 3.2.5博客开发教程:体验django模板
- 【1】按照Django官网,编写一个web app 创建project/配置数据库
- Django学习11 -- Admin页面级联选择
- Django===django工作流
- Django项目在linux系统中虚拟环境部署
- django 数据库连接模块解析及简单长连接改造