aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/locking
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2012-01-19 09:38:37 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2012-01-19 09:39:20 -0800
commit4c5b73fef8a41bd2bd8435fa4b00f7c40b721650 (patch)
treee365d2a2478ef3c81baecea85c9aff638509f4ca /activerecord/lib/active_record/locking
parent00554568e0625b2e4d4212ab7464f02234f74427 (diff)
downloadrails-4c5b73fef8a41bd2bd8435fa4b00f7c40b721650.tar.gz
rails-4c5b73fef8a41bd2bd8435fa4b00f7c40b721650.tar.bz2
rails-4c5b73fef8a41bd2bd8435fa4b00f7c40b721650.zip
Merge pull request #4531 from exviva/pessimistic_with_lock
Add ActiveRecord::Base#with_lock
Diffstat (limited to 'activerecord/lib/active_record/locking')
-rw-r--r--activerecord/lib/active_record/locking/pessimistic.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/locking/pessimistic.rb b/activerecord/lib/active_record/locking/pessimistic.rb
index 66994e4797..58af92f0b1 100644
--- a/activerecord/lib/active_record/locking/pessimistic.rb
+++ b/activerecord/lib/active_record/locking/pessimistic.rb
@@ -38,6 +38,18 @@ module ActiveRecord
# account2.save!
# end
#
+ # You can start a transaction and acquire the lock in one go by calling
+ # <tt>with_lock</tt> with a block. The block is called from within
+ # a transaction, the object is already locked. Example:
+ #
+ # account = Account.first
+ # account.with_lock do
+ # # This block is called within a transaction,
+ # # account is already locked.
+ # account.balance -= 100
+ # account.save!
+ # end
+ #
# Database-specific information on row locking:
# MySQL: http://dev.mysql.com/doc/refman/5.1/en/innodb-locking-reads.html
# PostgreSQL: http://www.postgresql.org/docs/current/interactive/sql-select.html#SQL-FOR-UPDATE-SHARE
@@ -50,6 +62,16 @@ module ActiveRecord
reload(:lock => lock) if persisted?
self
end
+
+ # Wraps the passed block in a transaction, locking the object
+ # before yielding. You pass can the SQL locking clause
+ # as argument (see <tt>lock!</tt>).
+ def with_lock(lock = true)
+ transaction do
+ lock!(lock)
+ yield
+ end
+ end
end
end
end