[Dbix-class] is it safe use find_or_create in "read committed" transaction level

Bill Moseley moseley at hank.org
Mon Sep 20 06:01:00 GMT 2010


On Sun, Sep 19, 2010 at 9:08 PM, Wei Gui <guienator at gmail.com> wrote:

> I start use DBIx in my project. and I read this from
> DBIx::Class::ResultSet document
>
> >> Note: Because find_or_create() reads from the database and then possib=
ly
> inserts based on the result, this method is subject to a race condition.
> Another process could create a record in the table after the find has
> completed and before the create has started. To avoid this problem, use
> find_or_create() inside a transaction.
>
> IMO, put find_or_create inside a transaction only works if the
> transaction level is serializable. But for Oracle whose default
> transaction level is "read committed", which means race condition in
> find_or_create may still happen even in a transaction.
>

I find that comment in the docs vague, too.  Postgresql, likewise, uses read
committed as the default.

Would a "create_or_find()" method solve that problem?  I've got code that
attempts to insert first, and on duplicate key violation I then fetch the
row.



-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20100919/955=
93895/attachment.htm


More information about the DBIx-Class mailing list