Re: Overwrite active_record find?

Christian Kerth
Thu, 29 May 2008 13:05:08 -0000

Frederick Cheung wrote:

> Should have been obvious - it looks like you''re not returning the
> right value (ie the result) from find_by_sql. Also bear in mind that
> find will call find_by_sql in some cases, so make sure whatever
> locking mechanism you''re using will support that. I''ve
got a hint of a
> doubt that by background task you mean a separate process, in which
> case you''re out of luck (because each process would have its own
lock).

I switched the locking mechanism to thread.exclusive, because a mutex
doesn''t work because find is calling find_by_sql. Added the returns.

def self.find(*args)
           begin
                #@@semaphore.synchronize {
                    Thread.exclusive {
                        #puts "find"
                        return super(args)

                    }
                #}
            rescue
                puts "error: "+$!
               # @@semaphore.unlock
            end
        end

        def self.find_by_sql(sql)
           begin
                #@@semaphore.synchronize {
                    Thread.exclusive {
                      #puts "find_by_sql"
                      return super(sql)
                    }
                #}
            rescue
                puts "error: "+$!
               # @@semaphore.unlock
            end
        end


By background task is a second thread, not a seperate process, so the
locking mechanism should work.

Is every find call using find_by_sql to retrieve data? Would it be
enough to syncronize just find_by_sql then?

thx
--
Posted via http://www.ruby-forum.com/.