﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33497	Document that database persistent connections do not work with ASGI nor async mode	Stenkar	Carlton Gibson	"Hello!

I've discovered that after upgrading Django to ver. 4 (currently 4.0.2), I started to see database **FATAL:  sorry, too many clients already** errors in the Sentry.

For a database, I'm using containerized Postgres 14.1 and the connection between Django and Postgres is done by Unix socket.

Database settings look like this:
{{{
DATABASES = {
    ""default"": {
        ""ENGINE"": ""django.db.backends.postgresql"",
        ""NAME"": environ.get(""POSTGRES_DB""),
        ""USER"": environ.get(""POSTGRES_USER""),
        ""PASSWORD"": environ.get(""POSTGRES_PASSWORD""),
        ""HOST"": environ.get(""POSTGRES_HOST""),
        ""PORT"": environ.get(""POSTGRES_PORT""),
        ""CONN_MAX_AGE"": 3600
    }
}
}}}

In production, I'm using ASGI (Uvicorn 0.17.4) to run the Django application (4 workers).

When everything is deployed and I have surfed around the Django admin site, then checking Postgres active connections, using **SELECT * FROM pg_stat_activity;** command, I see that there are 30+ Idle connections made from Django.

After surfing more around the admin site, I can see that more Idle connections have been made by Django.

It looks like the database connections are not reused. At one point some of the Idle connections are closed, but then again more connections have been made when more DB queries are made by Django.

I have one Django 3.2.11 project running on production and all the settings are the same, there are always max 10 persistent connections with the database and everything works fine.

Should that be like this in version 4.0?"	New feature	closed	Database layer (models, ORM)	dev	Normal	fixed	ASGI, Database, async	Sarah Boyce Carlton Gibson Florian Apolloner Andrew Godwin Anders Kaseorg Patryk Zawadzki Mikail Alex joeli Marco Glauser Rafał Pitoń Marty Cochrane lappu Dmytro Litvinov Suraj Shaw Yiwei Gao	Ready for checkin	1	0	0	0	0	0
