﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
18557	get_or_create() causes a race condition with MySQL	Cal Leeming	nobody	"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"	Uncategorized	closed	Database layer (models, ORM)	1.4	Normal	duplicate		cal@… django@…	Unreviewed	0	0	0	0	0	0
