Since you are already working with transactions and since serializable 
transactions alone solve your example problem, I guess you can use the built-in 
support for specifying an isolation level for transactions that exists since 
Rails 4 [1]. Do you still need any extra pessimistic locking given that this 
support exists? If so, what kind of a scenario would still need it?

[1]: 
https://github.com/rails/rails/commit/392eeecc11a291e406db927a18b75f41b2658253

--
Ufuk Kayserilioglu


On 5 January 2016 at 15:46:33, [email protected] 
([email protected]) wrote:

Here is one example from production - an order has a fulfillment - the 
fulfillment can be deleted if it is not 'shipped', very roughly: 

# update
transaction do
  fulfillment.status = 'shipped'
  fulfillment.save!
  StockMovement.new(fulfillment).save
end  

# delete
transaction do
  fulfillment.destroy # validates that fulfillment.status = 'unshipped'  
end

Since StockMovements are not real foreign keys, nothing prevents the 
fulfillment from being deleted by a dyno handling a delete request even if 
another dyno has already created stock_movements since it's validating with 
stale data.  

This is just one of a number of cases of an otherwise very normal transactional 
process breaking since Rails does validation and relational logic on the ruby 
level (not supporting real database foreign keys) - which means pessimistic 
locking or isolation level serializable are a must. 


On Monday, January 4, 2016 at 8:34:03 PM UTC+7, Xavier Noria wrote:
On Sun, Jan 3, 2016 at 1:19 PM, <[email protected]> wrote:

Unfortunately optimistic locking simply does not provide any real guarantees 
once you have scaled past a single dyno

Can you elaborate?
 
--
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to