mercredi 7 octobre 2015

Strong consistency for a Rails model in Mongoid 3

I want all of my db interactions for a specific model to go through the mongo primary in my cluster, so I set the model to use strong consistency.

class Photo
  include Mongoid::Document
  with consistency: :strong

  field :number, type: Integer
  # let's say a photo number is unique in the db
  validate :unique_number
end

But this does not seem to work, because I still run into validation errors when I save two Photo photos very close together.

photo1 # db has number=1 for this object
photo1.update_attributes(number: 2)
photo2.number = 1
photo2.save! # <= this raises a validation exception

My understanding of strong consistency is that there shouldn't be a race here. It should do the write and then do the read, and since it's all off the primary there shouldn't be a conflict. What am I missing?

Aucun commentaire:

Enregistrer un commentaire