Rails 3 - Potential Race Condition? -


i have simple rails 3 application users can reserve 1 of finite number of homogeneous items particular day. i'm trying avoid race condition 2 people reserve last item available on particular day. model (simplified) follows:

class reservation < activerecord::base   belongs_to :user   attr_accessible :date   max_things_available = 20    validate :check_things_available    def check_things_available     unless things_available? errors[:base] << "no things available"   end    def things_available?     reservation.find_all_by_date(date).count < max_things_available    end            end 

the reservation being created in controller via current_user.reservations.build(params[:reservation])

it feels there better way this, can't quite put finger on is. on how prevent race condition appreciated.

not sure answers question, might point towards solution:

http://webcache.googleusercontent.com/search?q=cache:http://barelyenough.org/blog/2007/11/activerecord-race-conditions/

(the original site seems down that's link google cache)

the conclusion on page optimistic locking , row level locking not solutions race conditions on create, on update.

the author suggests reimplementing find_or_create db constraint.

another suggestion switching transaction isolation level 'serializable' ought work there's no information on how in rails.


Comments

Popular posts from this blog

jQuery Ajax Render Fragments OR Whole Page -

javascript - Iterate over array and calculate average values of array-parts -

java - Simple Command Line calculator -