#35455: psycopg3 warns about connections not being closed
-------------------------------------+-------------------------------------
     Reporter:  HTErik               |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  5.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  psycopg              |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by HTErik):

 I have identified the problem now.
 It happens when one performs Django queries from a separate thread. The DB
 connections in Django are thread local inside the `ConnectionHandler`, so
 when the thread finishes and is eventually GCd, the thread locals holding
 the DB connections in Django are also GCd, without any prior `close()`.

 To solve it, one must call this in a `finally` clause of all threads that
 run Django queries.

 {{{
 for conn in django.db.connections.all():
     conn.close()
 }}}

 I'm not sure if this is expected from users or if this is something Django
 should handle internally.
 The documentation on this topic could use some more details.
 https://docs.djangoproject.com/en/5.0/ref/databases/#connection-management
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35455#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018f7b6e7f0f-4b6f92c3-fd41-4842-be13-45eb304da679-000000%40eu-central-1.amazonses.com.

Reply via email to