Stale record in multiprocess environment
I have a database that is shared among possible different Ruby on Rails processes. To keep consistency, I would like the record I operate on is up-to-date.
I am hoping to solve this problem: process A reads a record, process B reads the same record, p开发者_JAVA百科rocess A update the record, (now the instance of record in process B is stale). Since there are multiple processes involved, I clearly can not use any mutexes, etc.
Is there any good way to do this?
I don't have much experience with this, but I would look into Transactions and locking.
http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html
http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html
http://api.rubyonrails.org/classes/ActiveRecord/Locking/Pessimistic.html
Optimistic locking sounds like the best fit for what you're doing.
精彩评论