修复first_or_initialize / first_or_create比赛

时间:2014-09-05 15:24:21

标签: ruby-on-rails postgresql activerecord

基本上我发现所有“first_or_initialize”/“first_or_create”调用都有竞争条件。

正常情况:

server1: first_or_initialize -> initialize
server1: save new record
server2: first_or_initialize -> first
server2: update old record

种族案例:

server1: first_or_initialize -> initialize
server2: first_or_initialize -> initialize
server1: save new record
server2: save new record **ERROR:  duplicate key violates unique constraint**

我该如何解决?

0 个答案:

没有答案