[Dbix-class] Issue with DBIx::Class::ResultSet::find_or_create():

Matt S Trout dbix-class at trout.me.uk
Sat Jul 28 09:40:14 GMT 2007


On Fri, Jul 27, 2007 at 06:38:19PM -0700, Mesdaq, Ali wrote:
> This came up in the Catalyst list today so I am posting here for
> discussion or bug reporting.
> 
> The issue is that it seems the find_or_create() call can have a hiccup
> in high load situations where find_or_create is called more than once at
> the same time with the same value. So what happens is both calls try to
> do a find and return nothing then they attempt to insert and once
> succeeds and one gets an error of:
> 
> DBIx::Class::ResultSet::find_or_create(): DBI Exception:
> DBD::mysql::st execute failed: Duplicate entry

My initial response to this was the same as everybody else's, "catch the damn
exception".

My second response was "can't we make this atomic somehow?" ... but of course
the answer is no. We could do transaction stuff and ... throw a different
exception.

I don't like your "return -1" approach though.

Three possiblities:

(1) trap duplicate key exception and do what you describe
    - do DBs give a sanely trappable exception here?

(2) try the re-select no matter what the exception
    - actually -probably- safe. performance issues?

(3) 1, but don't bother with the initial select call, try the insert and then
    find if it fails
    - would this actually be faster?

Thoughts, people?) try the re-select no matter what the exception
    - actually -probably- safe. performance issues?

(3) 1, but don't bother with the initial select call, try the insert and then
    find if it fails
    - would this actually be faster?

Thoughts, people?

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director    Want a managed development or deployment platform?
 Shadowcat Systems Ltd.  Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/             http://www.shadowcatsystems.co.uk/ 



More information about the Dbix-class mailing list