mm-optimistic_locking implements optimistic locking in mongo_mapper.
When attempting to save a record, optimistic locking verifies that the record has not been modified since it was earlier loaded into memory. If it has been modified and resaved since that time, an error is raised and the record must be reloaded and resaved again.
Optimistic locking is appropriate when the chance of a conflict is low and records can easily recover when a conflict does occur (e.g., simply by retrying the operations or by asking an end user to manually resolve the conflict).
Before using mm-optimistic_locking, you should first consider if your use case for locking can be handled by MongoDB's various atomic operations. It is possible that you do not need this type of locking at all.
If using Bundler (e.g., with Rails 3), add the gem to your project's Gemfile:
MongoMapper::Plugins::OptimisticLocking in each model
that needs optimistic locking functionality.
class BlogPost include MongoMapper::Document # Add the following line to each model that needs optimistic locking plugin MongoMapper::Plugins::OptimisticLocking end
Finally, make sure to rescue from
any time you save a model with optimistic locking functionality:
begin blog_post.save rescue MongoMapper::StaleDocumentError # Reload, remodify, and resave end