#15802: Django stops functioning when the database (PostgreSQL) closes the
connection
-------------------------------------+-------------------------------------
     Reporter:  Rick.van.Hattem@…    |                    Owner:
         Type:  Bug                  |  Honza_Kral
    Component:  Database layer       |                   Status:  reopened
  (models, ORM)                      |                  Version:  1.3
     Severity:  Normal               |               Resolution:
     Keywords:  database, postgres,  |             Triage Stage:  Accepted
  postgresql, connection, closed     |      Needs documentation:  0
    Has patch:  1                    |  Patch needs improvement:  1
  Needs tests:  0                    |                    UI/UX:  0
Easy pickings:  0                    |
-------------------------------------+-------------------------------------
Changes (by akaariai):

 * cc: anssi.kaariainen@… (added)
 * needs_better_patch:  0 => 1


Comment:

 In normal HTTP serving situations the connection doesn't get stuck into
 closed state. When the request is finished, the connections are closed and
 the already committed .close() fix will remove the broken connection. I am
 of course talking about 1.4 here, as it has the .close() fix already
 applied.

 However, if using the connection in a long running task, then there will
 be problems. In that setting, the connection is usually never closed. We
 should really document that you should call connection.close() after you
 are done with it. Actually, we should document how to safely use Django's
 connections in task-running setting, I don't think we have that and there
 are multiple potential problems in that setting (Idle in TX for example).

 The attached patch doesn't apply cleanly (15802-2.diff). It doesn't apply
 to the base directory, and even if applied into the django/ directory, it
 doesn't apply cleanly. In addition, if I am reading it correctly it is
 downright dangerous. I think what happens is this.
 {{{
 obj.save() # succeeds
 # connection is closed for some reason
 obj.save() # Hey, ._cursor() gave me a new connection! No error raised
 anywhere, perhaps I am even in a different transaction...
 }}}

 I don't believe there to be any safe way to automatically create a new
 connection without the user calling .close() in between. Surprising things
 could happen otherwise. The only possible exception would be that when
 leaving transaction management the connection could be closed on error.
 This could also help in many situations, because usually when you get an
 error (possibly due to closed connection), you will rollback the
 transaction (leave transaction management), and thus the connection would
 get unstuck.

 Still one helpful thing would be to return a consistent
 `ConnectionClosedError` when the connection is closed outside Django's
 control.

 In short: I don't think there is anything to do to get this into 1.4. Too
 big changes required. The only solution you have is somehow detecting
 closed connections in your code and closing the connection manually in
 those cases.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/15802#comment:20>
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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to