No commit activity in last 3 years
No release in over 3 years
Before a record is saved, mm-optimistic_locking will check if it has been modified by another process. If so, a StaleDocumentError will be raised. The object can be reloaded and resaved after the conflict has been resolved.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.8.7
~> 2.6.0

Runtime

 Project Readme

mm-optimistic_locking

Build Status

mm-optimistic_locking implements optimistic locking in mongo_mapper.

Optimistic Locking?

When attempting to save a record, optimistic locking verifies that the record has not been modified by another process 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.

Quickstart

If using Bundler (e.g., with Rails 3), add the gem to your project's Gemfile:

gem 'mm-optimistic_locking'

And run bundle install.

Next, include 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 MongoMapper::StaleDocumentError any time you save a model with optimistic locking functionality:

begin
  # .. making modifications to blog_post ..
  blog_post.save
rescue MongoMapper::StaleDocumentError
  # Reload, remodify, and resave
  # e.g.:
  blog_post.reload
  retry
end

Related Projects

Contributing

  • Fork the repository on GitHub
  • Create a feature branch (a.k.a., create a branch named my-awesome-feature)
  • Write the new feature (please also add specs)
  • Make sure all specs pass: rake spec
  • Submit a pull request that only includes your feature branch

MIT License

Copyright (c) 2011 Highgroove Studios

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme.meme