Hello André, > Is there a way to force Django to close the DB connection after each operation (unless we're in an atomic block)?
The only way I can think of achieving that is by bundling your own database backend subclass and override execute() and the cursor subclass to close the connection when appropriate. Is there any reason you opted for an in-application connection pool instead of using pgbouncer or pgpool instead? Best, Simon Le lundi 14 mai 2018 13:02:15 UTC-4, an...@cabine.org a écrit : > > Hello. > > For the last 6 years I've been using gevent's greenlets to serve Django > requests, instead of tying up a thread or a process, and this has served me > well for web applications that deal with slow streaming clients, or have to > call some slow external services mid-request, or are just IO-bound in > general. However, when I do have to use the Django ORM, it sort of throws a > wrench in my gears. > > The default behavior is to open a new DB connection as soon as it is > needed while servicing a request, and close it at the end of the request. I > don't really want to have a DB connection open for each concurrent request > being served (using greenlets it is possible to serve a lot of requests > concurrently), and I don't trust that every single one will be correctly > closed at the end either (https://code.djangoproject.com/ticket/29069 for > example), which would cause a leak. Also, managed DB services typically > have a limited amount of DB connections that we can use so I need to limit > this number. > > I have been using a connection pool ( > https://github.com/lcd1232/django-postgrespool2) in order to keep the > number of DB connection under control, but this relies on Django closing > the connection in order for it to be returned to the pool, and therein lies > the rub. Django will only close the DB connection at the end of the > request. So, in practice, if my requests make use of the ORM my concurrency > is limited by the size of the connection pool. Is there a way to force > Django to close the DB connection after each operation (unless we're in an > atomic block)? > > I know there are other options such as relying on external components > like pgbouncer or Pgpool-II but I would prefer not to add yet another > component to my architecture. And this would also mean that a LOT of > connections would need to be managed between Django and this component as > well... > > Thank you and best regards, > André > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/744abaf7-b087-4fab-b56d-658cec84b4e7%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.