开发者

ruby on rails concurrent adds

开发者 https://www.devze.com 2023-03-13 08:47 出处:网络
I have a model with a has_many association. The association also has an :after_add component which increments a counter. I was wondering how this would play out in a concurrent scenario (e.g. when two

I have a model with a has_many association. The association also has an :after_add component which increments a counter. I was wondering how this would play out in a concurrent scenario (e.g. when two different instances are added to the has_many at the exact same time). Would the counter increase by 2? Does rails internally get a lock, increment, and release the lock? Is the add and :after_ad开发者_JAVA技巧d callback seen as one atomic action?

Thanks.


As long as you're using the increment methods and not doing a SELECT, increment in Ruby, then UPDATE, you should be fine. More info: rails 3 & activerecord: do I need to take special record locking precautions to update a counter field?

0

精彩评论

暂无评论...
验证码 换一张
取 消