Skip to content

Commit

Permalink
Fix a condition which would lead to MySQL deadlock
Browse files Browse the repository at this point in the history
Running a delete by an index condition, for example:

```
DELETE FROM spree_adjustments
  WHERE adjustable_id = 1
  AND adjustable_type = 'Spree::Shipment'
```

will require MySQL to acquire a gap lock on the index which indexes
adjustable_id and adjustable_type.

Running this in a heavily loaded environment can lead to deadlocks in
MySQL as multiple threads need an exclusive gap lock on the index in
question.

By deleting records by the primary key instead, MySQL is able to deal
with this without requiring an exclusive lock on the primary key index,
avoiding the deadlock situation.
  • Loading branch information
Gregor MacDougall committed Jun 30, 2015
1 parent 90beea3 commit 07f9983
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ def ensure_shipping_address
end

def create_proposed_shipments
adjustments.shipping.delete_all
adjustments.shipping.destroy_all
shipments.destroy_all
self.shipments = Spree::Stock::Coordinator.new(self).shipments
end
Expand Down

0 comments on commit 07f9983

Please sign in to comment.