#18557: get_or_create() causes a race condition with MySQL
-------------------------------+--------------------
     Reporter:  foxwhisper     |      Owner:  nobody
         Type:  Uncategorized  |     Status:  new
    Component:  Core (URLs)    |    Version:  1.4
     Severity:  Normal         |   Keywords:
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+--------------------
 Hi,

 When using MySQL, get_or_create() has a race condition under high load
 scenarios.

 Up until now, the only fix was to use READ COMMITED transaction isolation,
 but this can break legacy apps.

 Instead - we have been using the following fix in production, and it works
 great under high load or multi threaded / multi node job queues.

 {{{
 # This needs uploading tomorrow
 class ExtendedManager( Manager ):
     @transaction.commit_on_success
     def get_or_create(self, *args, **kwargs):
         transaction.commit()
         created = None
         try:
             return super(ExtendedManager, self).get_or_create(*args,
 **kwargs)

         except IntegrityError, e:
             transaction.commit()
             print "RACE 3: %s, %s" % ( str(e) , kwargs)
             # Ensure the error code matches 1062 (duplicate entry)
             if not e.args[0] == 1062:
                 raise e

             _res = self.all().filter(**kwargs)
             if not _res:
                 raise Exception, "Object busy or not yet ready: %s" % ( e
 )
             if len(_res) > 1:
                 raise Exception, "get_or_create(): duplicate object found,
 this should never happen"

             return _res[0], False
 }}}


 This has been discussed here:
 http://stackoverflow.com/questions/2235318/how-do-i-deal-with-this-race-
 condition-in-django

 And here:
 https://groups.google.com/forum/?fromgroups#!topic/django-developers/VNpt-
 sxSmho

 Is there any chance this patch would make it into the core?

 Cal

-- 
Ticket URL: <https://code.djangoproject.com/ticket/18557>
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