[CDBI] atomic find_or_create()

Perrin Harkins perrin at elem.com
Sat Oct 22 01:33:08 BST 2005


Has anyone ever tried to make an atomic version of find_or_create()?
The existing version has a race condition in that someone else could
create a record with the specified primary key in between the SELECT and
INSERT.

I need this on MySQL InnoDB and I was considering a couple of
strategies.  The most obvious one is to lock the table (in shared mode)
while doing the normal find_or_create().  Less obvious would be to use
INSERT IGNORE followed by a SELECT.  This could cause strange results if
you pass in search criteria that don't form a unique key, but that's
true for the existing method anyway.

Anyone else have thoughts on this?

- Perrin





More information about the ClassDBI mailing list