Re: Uniqueness and sql serialize

19 views
Skip to first unread message

Matt Jones

unread,
Oct 26, 2012, 8:21:16 AM10/26/12
to rubyonra...@googlegroups.com


On Wednesday, 24 October 2012 18:26:15 UTC-4, Filipe Giusti wrote:
Hi fellow programmers,

I was looking for how to keep integrity in some financial transactions and I saw this "This could even happen if you use transactions with the ‘serializable’ isolation level." in the uniqueness page http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_uniqueness_of. How this is possible with the serializablo isolation level? Aren't the select and update done in the same transaction?


They are, but that doesn't prevent races like the one in the documentation from occurring - in that case, both transactions find no conflicting records and create a row.

As noted in the docs, if you really really need data consistency you're better off using a unique index and catching ActiveRecord::RecordNotUnique.

--Matt Jones
Reply all
Reply to author
Forward
0 new messages