# How Does Locking Work?

**URL:** <https://www.ruby-forum.com/t/how-does-locking-work/177361>\
**Category:** Rails\
**Created:** [October 15, 2009, 3:58pm UTC](https://www.ruby-forum.com/t/how-does-locking-work/177361 "2009-10-15T15:58:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gers4302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c2a13f/32.png) [@gers4302](https://www.ruby-forum.com/u/gers4302)\
**Post date:** [October 15, 2009, 3:58pm UTC](https://www.ruby-forum.com/t/how-does-locking-work/177361/1 "2009-10-15T15:58:24Z")

</div>

I’ve been reading about locking for ActiveRecord, but it seems a  
little vague to me. My situation is this: I have a controller that  
assigns jobs to people who are logged in. The basic code is:

j = Job.find :first, :conditions =\> ‘employee\_id is null’  
unless j.nil?  
j.employee = current\_employee  
j.save  
end

Of course, this can cause, and has caused a race condition. My  
temporary fix was to add :order =\> ‘RAND()’, so that the possibility  
of a collision is reduced, but the possibility is still there.

It seems the lock! might fix this, but I don’t understand what really  
happens here. If I lock! the record, what happens to the second  
employee that tries to pull the record and save it?

Is there another method I should be looking at?

---

<div class="post-metadata">

**Author:** ![gers4302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c2a13f/32.png) [@gers4302](https://www.ruby-forum.com/u/gers4302)\
**Post date:** [October 15, 2009, 4:00pm UTC](https://www.ruby-forum.com/t/how-does-locking-work/177361/2 "2009-10-15T16:00:10Z")

</div>

On Oct 15, 2:34 pm, stack [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Of course, this can cause, and has caused a race condition. My  
> temporary fix was to add :order =\> ‘RAND()’, so that the possibility  
> of a collision is reduced, but the possibility is still there.
> 
> It seems the lock! might fix this, but I don’t understand what really  
> happens here. If I lock! the record, what happens to the second  
> employee that tries to pull the record and save it?

lock! is a database level lock (ie select … for update). You get an  
exclusive lock on that row that lasts for the duration of the current  
transaction (corollary: if you do this outside a transaction it  
basically does nothing). If something else tries to read or update  
that row that attempt will block until the lock is released (or the  
thing waiting for the lock gets bored of waiting)

> Is there another method I should be looking at?

Rails’ optimistic locking may also be relevant

Fred
