我注意到 Rails 可能存在多台服务器的并发问题,并且希望强制我的模型始终锁定。这在 Rails 中是否可能,类似于强制数据完整性的独特约束?还是只需要仔细编程?
一号航站楼
irb(main):033:0* Vote.transaction do
irb(main):034:1* v = Vote.lock.first
irb(main):035:1> v.vote += 1
irb(main):036:1> sleep 60
irb(main):037:1> v.save
irb(main):038:1> end
二号航站楼,睡觉时
irb(main):240:0* Vote.transaction do
irb(main):241:1* v = Vote.first
irb(main):242:1> v.vote += 1
irb(main):243:1> v.save
irb(main):244:1> end
数据库启动
select * from votes where id = 1;
id | vote | created_at | updated_at
----+------+----------------------------+----------------------------
1 | 0 | 2013-09-30 02:29:28.740377 | 2013-12-28 20:42:58.875973
执行后
一号航站楼
irb(main):040:0> v.vote
=> 1
二号航站楼
irb(main):245:0> v.vote
=> 1
数据库结束
select * from votes where id = 1;
id | vote | created_at | updated_at
----+------+----------------------------+----------------------------
1 | 1 | 2013-09-30 02:29:28.740377 | 2013-12-28 20:44:10.276601
其他示例
http://rhnh.net/2010/06/30/acts-as-list-will-break-in-production