Django multitenant using middleware - can you persist connections

I'm using django with django-tenants for a SaaS app and it is using the user's subdomain to set the db schema in postgresql. So a call to user1.app.com will use the 'user1' schema.

Each call to a view creates a new database connection by default. If the view call ajax, a new connect/disconnect happens. Using this method works as each call gets it's postgresql search path set to the correct schema using the django-tenants middleware.

My question is if I set CONN_MAX_AGE to persist connections for a period of time to reduce the heavy connect/disconnect load on the server, will django pool connections from multiple users potentially using different schemas? I am concerned, because:

  1. All users are using the 'default' DATABASE. (just the search path changes)
  2. I don't see any logic that binds a connection to a unique user session.
  3. The built in development server ignores the CONN_MAX_AGE setting so I can't test locally.
  4. I have looked into the connection_created signal, but I would be duplicating logic with django-tenants and I'm not sure of any unintended consequences.

Using django 4.0.7 and posgresql 12

Any insight or suggestions are appreciated.

Back to Top