Ruby on Rails Tuesday, November 2, 2010

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.14 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJM0HjHAAoJEP5F5V2hilTWYx4H/i2dS6t+kyqvA2PAFHzJCAn/
MtGSq09M4v1fYUSUdGPVquAN0b6QQ83navhfGvZ4BgNp9N8jnZuAHarJioTds3iz
bLxY3UJkRFLMdz7DfnekAbQO+7mqmTN0SggRkA4BjqsudWa24OF2+b5XbcWFRhWX
gdw6oETgkzUk1eQsfLcbCC6Ei/t4aHNvm/rfxNkjYQUPkQY1fYTwpYkVh43kxCX4
TU7mlAQ7nIaYiadxl/78UYNto1cx60e1JQbh6XWsNJeWNHs7Q35Tg4yt9zY5Z5h9
MJXS4Yd/x/07rpdVYeMMGjuhGiM9FCX4qN3J5QQkzEIY9WQIp2dthBbTTrbua0U=
=Za6/
-----END PGP SIGNATURE-----
On 11/2/10 4:17 PM, Marnen Laibow-Koser wrote:
>
> I think you do. If I remember correctly, :lock doesn't do any actual
> DB-level locking -- it just sets a lock field that ActiveRecord expects.
> Transactions, OTOH, *do* do DB-level locking, but shouldn't introduce
> read locks for the sort of operations you're doing. And you're not
> doing any DB writes here.
>

:lock is supposed to do row-level locking via the DB facilities:

http://api.rubyonrails.org/classes/ActiveRecord/Locking/Pessimistic.html

I believe it's the optimistic locking that uses the lock field in active
record, and throws a StaleObjectException if things have been modified.

Anyway, we discovered what the problem was. So for the sake of the
archives...

:lock won't block on a standard SELECT. Only SELECT FOR UPDATE.

So if I run this code in terminal one:

Foo.transaction do
f = Foo.find(1, :lock => true)
1.upto(10000000) { |i| puts i }
end

And this in terminal two while terminal one is spitting out numbers:

Foo.transaction do
puts Foo.find(1, :lock => true).inspect
end

The second process will block, even for that read-only operation, until
the transaction in the first process ends.

--
Grant

"I am gravely disappointed. Again you have made me unleash my dogs of war."

No comments:

Post a Comment