1

I'm currently doing a pessimistic loking with rails 3 + postgresql. But there seems to be no way to confirm that the lock is working unless I go through the hassle of making a concurrent test. Is there no way to test this via console?

Example

User.transaction do 
  u1 = User.find(1, :lock => true)
  u2 = User.find(1)
  ## u2 should not be able to do anything right?
end

1 Answers1

8

Open 2 consoles

Console 1:

User.transaction do 
  u = User.find(1, :lock => true)
  sleep(30)
end

Once that is executed switch to console 2 then do this

Console 2:

u = User.find(1)
u.name = "new name"
u.save!

You will then see that console 2 will not commit it's update not until the 30 second sleep on console 1 is finished.

Berimbolo
  • 293
  • 2
  • 12