how does CONN_MAX_AGE work in Django

2019-06-21 16:00发布

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).

0条回答
登录 后发表回答