Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Implements optimistic locking (similar to ActiveRecord) for MongoMapper

tag: v0.0.1

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 mm-optimistic_locking.gemspec
README.md

mm-optimistic_locking

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 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
  blog_post.save
rescue MongoMapper::StaleDocumentError
  # Reload, remodify, and resave
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
Something went wrong with that request. Please try again.