views:

15

answers:

1

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, process 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?

A: 

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.

Karl