Thank you, I also believe it is Race condition. I do not want to use 
get_or_create as I like to have control over DB writes and updates. I will 
try Unique_together and catch the integrity error exception as Kurtis 
suggested or check again before doing save().

or just thinking aloud, Is there a way to acquire a lock which can 
serialize any other requests which have same "a" and "b" values?



On Friday, June 22, 2012 7:52:49 AM UTC-7, Daniel Roseman wrote:
>
> On Friday, 22 June 2012 06:23:32 UTC+1, ydjango wrote:
>>
>> I have code as
>>
>> view_a(request):
>>   .... Populate my_a, my_b and my_c...
>>   try:
>>       tp = TP.objects.get(a__exact = my_a, b__exact = my_b)
>>   except TP.DoesNotExist, e:
>>      tp = TP()
>>      tp.a = my_a
>>      tp.b = my_b
>>      tp.c = my_c
>>      tp.save()
>>   else:
>>      tp.c = my_c
>>      tp.save()
>>
>> This view is called by ajax calls. TP is a django model and mysql db. 
>> I am using django 1.1.4/ python2.5/mod_wsgi2.5,
>>
>> I find that in, one case, two rows with same value of  a and b has been 
>> created in DB. ( value of c was different)
>> What could have caused it? 
>>
>> Any clues or hints on how to track down this problem or what to look for 
>> will be appreciated.
>>
>>
> You probably have a race condition. You should use the built-in 
> `get_or_create` manager method, which helps to minimize that sort of thing 
> (it's not perfect, but it's better than coding it yourself).
> --
> DR.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/6XYOOu3cmIoJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to