I'm finally back to Vitória, after a great time in RubyConfBR, even though I've been target of jokes from Aaron Patterson regarding Rspec questions due my questions to David Chelimsky in a non-related talk from him. But that's ok, it was fun and I deserved it :)

When listening to Yehuda's talk about development on the client side, which was really great by the way, I got really worried when he commented about some Rails validations not being concurrent-safe nor even thread-safe.

While I can understand it is hard to guarantee uniqueness validation among different server instances, this can be easily avoided in a single server configuration with config.threadsafe! enabled.

Actually, I can't understand why thread-safe isn't enabled by default in production, since we have much better thread support in MRI 1.9.2 and always had on JRuby.

I've just read the documentation for validates_uniqueness_of and it explains well the problem:

http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_uniqueness_of

But I was thinking if Rails could provide some way for avoiding dealing with exceptions when using a single multi-thread server environment. For instance:

@record.validate :lock => @shared_lock do
  # code run if @record.valid?
  ...
  @record.save
end or (render :edit; return)
redirect_to :list
...

The :lock parameter should be optional and an internal lock (one per model, maybe) should be used when not specified in the case it is not necessary to share this code with another block which could also affect the record, for instance.

An error should raise if 'config.threadsafe!' is not enabled and it should be pointed out in the docs that this won't work for multiple servers setup, for avoiding confusing end-developer users. Maybe a warning instead of an error should suffice, for allowing this usage by plugins that don't have control of the deployment decisions.

If the user calls 'save' directly without validating first, the validation and save operations shoud be atomic in this case. So, 'save' should also support the :lock parameter.

Is this reasonable or am I missing something?

Rodrigo.

--
You received this message because you are subscribed to the Google Groups "Ruby on 
Rails: Core" group.
To post to this group, send email to rubyonrails-c...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-core+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to