Class: Bookie::Database::Lock
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Bookie::Database::Lock
- Defined in:
- lib/bookie/database/lock.rb
Overview
Simulates table locks on databases that only have row locks
Based on kseebaldt.blogspot.com/2007/11/synchronizing-using-active-record.html
This should probably not be called within a transaction block. The transaction that this method creates uses the option :requires_new => true, limiting the negative effects of nested transactions, but concurrency safety is still strongly dependent on how the database engine handles locks.
Class Method Summary collapse
-
.[](name) ⇒ Object
Returns a lock by name.
Instance Method Summary collapse
-
#synchronize ⇒ Object
Acquires the lock, runs the given block, and releases the lock when finished.
Class Method Details
.[](name) ⇒ Object
Returns a lock by name
29 30 31 |
# File 'lib/bookie/database/lock.rb', line 29 def self.[](name) @locks[name.to_sym] ||= find_by!(name: name.to_s) end |
Instance Method Details
#synchronize ⇒ Object
Acquires the lock, runs the given block, and releases the lock when finished
17 18 19 20 21 22 23 |
# File 'lib/bookie/database/lock.rb', line 17 def synchronize transaction(:requires_new => true) do #Lock this record to be inaccessible to others until this transaction is completed. self.class.lock.find(id) yield end end |