Grant Olson wrote in post #958678:
> Hopefully I'm doing something stupid here. I was trying to use
> pessimistic locking on a rails 2.3.4 project and it just doesn't seem to
> work. The issue was showing up on both OSX and Linux.
>
> I've created a simplified reproducible for 2.3.10 for discussion
> purposes. I'll publish the full transcript of the steps I took to
> create the project later, but for now, I've got a single model Foo with
> a single Bar value that's a string. I create a single Foo and saved it.
>
> In one console window I run:
>
> Foo.transaction do
> f = Foo.find(1, :lock => true)
> 1.upto(10000000) { |i| puts i }
> end
>
> I was under the assumption I wouldn't be able to access that row at all
> until I finished printing out 10 million numbers. But while that is
> running, I can go to another console window and get to it:
>
> johnmudhead:locktest grant$ script/console
> Loading development environment (Rails 2.3.10)
>>> f = Foo.find(1)
> => #<Foo id: 1, bar: "AAAAAAAAAAAAAAA!", created_at: "2010-11-01
> 19:50:58", updated_at: "2010-11-01 19:50:58">
>
> That seems like I'm reading the row when I should be locked out of it.
>
> I have confirmed that I'm using the InnoDB backend on MySQL, which
> supports row level locking.
>
> Anyone have any advice or tips? Do I have a gross misunderstanding of
> pessimistic locks in ruby.
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.
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment