Row level locking not working as expected in rails/mysql -


i have following relation

parent has_many children  child  belongs_to parent 

and following code block

activerecord::base.connection.transaction   lock_acquired = true   if child.parent.lock! , child.parent.status == 1     child.parent.update_attributes(:status => 2)   else     lock_acquired = false   end    if lock_acquired    # other code follows here   end  end 

the above code block making sure 1 of children able change parent status @ time , hence lock! method , status check expression. somehow broken. though parent status changed 2 1 of children, child somehow able pass through if statement. there multiple processes running of course thought lock take care of that. looks lock given before transaction block completed.

may lock works differently expected. insights helpful.

thanks.

i'm not quite sure why updated code wouldn't work, there 1 odd thing it: you're using lock! though returns true/false value, can ever return model instance (if lock acquired) or raise error (if acquiring lock times out). means lock_acquired set false if lock is acquired, parent's status not equal 1.

this code might behave more predictably:

child.parent.with_lock   if child.parent.status == 1     child.parent.update_attributes(:status => 2)   end    # other code follows here end 

note with_lock behaves lock! -- either block executed, or lock timeout error thrown (after 50 seconds of waiting, under default mysql settings).


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -