﻿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
6641	Concurrency issue with get_or_create	JEFFGUINNESS	nobody	"This is from Travis Terry post on http://groups.google.com/group/django-developers/browse_thread/thread/9730f4005bf08b67 and where he is much more eloquent then I am, I will post his some of his comments.

Two threads/processes/servers (let's call them P1 and P2) need to
concurrently create a unique object
1. P1 calls get_or_create(), which tries to get the item (it doesn't exist)
2. P2 calls get_or_create(), which tries to get the same item (it doesn't exist)
3. P1's get_or_create() tries to create the item (this works and returns the item)
4. P2's get_or_create() tries to create the item.  One of two things happens:
    a. a second item is created with the same parameters if this doesn't violate a UNIQUE constraint
    b. the second create fails (because of a UNIQUE constraint) and raises an exception 

The following pseudo-code handles the problem:
{{{
def get_or_create(**kwargs):
    try:
       obj = get(**kwargs)
    except:
       try:
          obj = create(**kwargs)
       except:
          obj = get(**kwargs)
    return obj 
}}}"	Uncategorized	closed	Database layer (models, ORM)	dev	Normal	fixed	get_or_create	tomasz.zielinski@… gerdemb	Ready for checkin	1	0	0	0	0	0
