Re: best stategry: check before create

[prev] [thread] [next] [Date index for 2005/03/18]

From: Ofer Nave
Subject: Re: best stategry: check before create
Date: 22:04 on 18 Mar 2005
Perrin Harkins wrote:

>On Fri, 2005-03-18 at 00:34 -0800, Ofer Nave wrote:
>  
>
>>I appreciate the help, but I think I'm going to try a different 
>>strategy.  I think I'll explictly 'search' and then 'create' if the 
>>search yields 0 results.  Somehow that feels cleaner.
>>    
>>
>
>Keep in mind that there's a race condition in that strategy.  Someone
>could create a record between your search and create.  (This is true for
>find_or_create too.)  You need to either be prepared for possible (rare)
>failures in the create step, or lock the tables for the length of the
>search and create.
>
>- Perrin
>
>  
>
That's a good point.  Irrelevant in this case, since my script is the 
only writer, and only runs once every few minutes to update the 
database, but good to remember for other projects.  I suppose the only 
way to prevent this is to have a unique index on title, which I was 
considering doing.  I would slightly slow down inserts, but I do those 
so rarely that it would not be noticeble.

The other way to make this atomic is to write a stored procedure, but I 
hate those with a passion, and they are somewhat mutually exclusive with 
CDBI.

-ofer

best stategry: check before create
Ofer Nave 07:10 on 18 Mar 2005

Re: best stategry: check before create
Ofer Nave 07:14 on 18 Mar 2005

Re: best stategry: check before create
Jim Mozley 08:21 on 18 Mar 2005

Re: best stategry: check before create
Ofer Nave 08:34 on 18 Mar 2005

Re: best stategry: check before create
Perrin Harkins 15:58 on 18 Mar 2005

Re: best stategry: check before create
Ofer Nave 22:04 on 18 Mar 2005

Re: best stategry: check before create
=?ISO-8859-1?Q?Ask_Bj=F8rn_Hansen?= 07:22 on 18 Mar 2005

Re: best stategry: check before create
Ofer Nave 07:36 on 18 Mar 2005

Generated at 15:09 on 27 Mar 2005 by mariachi v0.52