#31315: Closing all connections in function tagged with @transaction_atomic or
transaction atomic block breaks atomicity
-------------------------------------+-------------------------------------
               Reporter:  David      |          Owner:  nobody
  Eling                              |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  2.1
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Bug might be related to https://code.djangoproject.com/ticket/21239

 If you use the transaction atomic tag on a function ala
 {{{
 @transaction.atomic
 def post(self, request):
    Machine.objects.create(name="a")
    connections.close_all()
    connection.connect()
    list(Machine.objects.all())
    Machine.objects.create(name="b")

    return JsonResponse({})
 }}}
 b will exist but a will not.

 Now the code above is way simplified compared to what actually caused the
 bug in my codebase but it replicates the issue (although it seems very
 arbitrary in its current form) The codebase in question is very complex
 and involves multiple databases on different machines, this is my best
 attempt at boiling it down to what bits caused what. So it calls
 connections.close_all() and then reopens the connection to the current db
 then does a query then later down the line it does a create.

 The issue will occur if you do this instead
 {{{
 def post(self, request):
    with transaction.atomic():
       Machine.objects.create(name="a")
       connections.close_all()
       connection.connect()
       list(Machine.objects.all())
       Machine.objects.create(name="b")

     return JsonResponse({})
 }}}

 During further testing it looks like swapping connections.close_all() with
 connection.close() still causes the issue to occur, so maybe the fix in
 the issue linked above did not fix the issue all the way.

 It's possible this bug is only happening due to the code exploiting some
 loophole or something but I just thought I would put this here just in
 case it might be a real problem that needs to be fixed. Within our code
 base we had to isolate the offending bit and do it before entering the
 atomic block.

 This is my first time reporting a django bug so I apologize for any
 issues.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31315>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.33406eaf6c6d90305b43e12ef2b87b37%40djangoproject.com.

Reply via email to