List Info

Thread: Making get_or_create (or equivalent) atomic on MySQL




Making get_or_create (or equivalent) atomic on MySQL
country flaguser name
United States
2007-07-31 21:47:41
Hi,

I'm trying to atomically check if a certain field in the db
exists and
create it otherwise. I naively started using get_or_create
but it
seems like that function does not try to do things
atomically at all.
My database is MySQL and my table type is InnoDB. I
installed the
select_for_update patch and was calling
select_for_update().get_or_create():

Tag.objects.select_for_update().get_or_create(name =
tagName)

This gave me a deadlock when run from multiple threads. I
also tried
select_for_update().get() with a try except on DoesNotExist
and then
calling create separately:

try:
	Tag.objects.select_for_update().get(name = tagName)
except Tag.DoesNotExist:
	Tag.objects.create(name = tagName)

I still get a deadlock. It appears that MySQL does not
create the
exclusive lock on the row in Tag because it does not exist
yet. Is
there a good way (simple or not) of performing this
operation
(get_or_create) atomically, even if it is MySQL specific.

I appreciate any help!

Thanks,
Can Sar


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )