[英]Django not reusing connections to MySQL with CONN_MAX_AGE=60
I am using Django 1.9.2 in development ( DEBUG=True
) with MySQL 5.6.23. 我在开发中使用Django 1.9.2(
DEBUG=True
)和MySQL 5.6.23。 Below is my database settings 以下是我的数据库设置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname',
'USER': "django",
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
'CONN_MAX_AGE': 60,
}
}
I am querying MySQL to get the number of active connections with the command below: 我正在查询MySQL以使用以下命令获取活动连接的数量:
show status where `variable_name` = 'Threads_connected';
It yield result like this 它产生这样的结果
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_connected | 10 |
+-------------------+-------+
1 row in set (0,00 sec)
Every time I make a new request to Django, the number of connected threads increase until I get (1040, 'Too many connections')
when the number Threads_connected=151
. 每次我向Django发出新请求时,连接线程的数量会增加,直到我得到
(1040, 'Too many connections')
,而Threads_connected=151
。 Furthermore, connections are not closed after 60s. 此外,60年代后连接不会关闭。
This behavior does not seem to happen in production ( DEBUG=False
). 生产中似乎不会发生此行为(
DEBUG=False
)。
You are running Django in development mode, so the CONN_MAX_AGE
is not effective because each request is served by a different thread. 您正在开发模式下运行Django,因此
CONN_MAX_AGE
无效,因为每个请求都由不同的线程提供服务。 From the docs : 来自文档 :
The development server creates a new thread for each request it handles, negating the effect of persistent connections.
开发服务器为它处理的每个请求创建一个新线程,否定持久连接的影响。 Don't enable them during development
在开发过程中不要启用它们
About the connection not being closed after 60 seconds: timed out connections are closed on the next request, so just make a new request to django after more than 60 seconds, it should detect the obsolete connection and close it. 关于60秒后未关闭的连接:超时连接在下一个请求时关闭,因此在超过60秒后向django发出新请求,它应该检测到过时的连接并关闭它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.