how does CONN_MAX_AGE work in Django

我与影子孤独终老i 提交于 2019-12-10 14:33:39

问题


can someone ELI5 what CONN_MAX_AGE does? I thought it worked like this:

1) Request #1 comes in, opens connection 1 to database

2) Request #1 uses connection 1 to do some work

3) Request #1 completes. Because CONN_MAX_AGE is non-zero (and the age has not been reached), the connection is left open.

4) Request #2 comes in, and Django re-uses connection #1 to the database.

But that doesn't seem to be happening. I have a page on my site that does an AJAX poll every 15 seconds. In my development environment, I see the number of open connections (select count(*) from pg_stat_activity), slowly grow, until eventually I get

OperationalError: FATAL:  sorry, too many clients already

So I'm wondering where I've gone awry. Is CONN_MAX_AGE only used to keep connections open within a single HTTP request?

UPDATE:

Looking more carefully at the docs, I see this:

The development server creates a new thread for each request it handles, negating the effect of persistent connections. Don’t enable them during development.

Ah, so that seems to imply that a connection "belong to" a thread. (And the thread may open/close the connection, based on the value of CONN_MAX_AGE).

来源:https://stackoverflow.com/questions/41816570/how-does-conn-max-age-work-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!