diff options
author | Olek Janiszewski <olek.janiszewski@gmail.com> | 2012-01-18 23:03:55 +0100 |
---|---|---|
committer | Olek Janiszewski <olek.janiszewski@gmail.com> | 2012-01-18 23:23:41 +0100 |
commit | 7afbc89c37e56531c9ef4e34369e329aab1b21de (patch) | |
tree | b2647a40311d813c21712dd5f78fe53f7b76ede8 /activerecord/test | |
parent | 423b2626d85f75bb5fec03909ff8963bded7c7d5 (diff) | |
download | rails-7afbc89c37e56531c9ef4e34369e329aab1b21de.tar.gz rails-7afbc89c37e56531c9ef4e34369e329aab1b21de.tar.bz2 rails-7afbc89c37e56531c9ef4e34369e329aab1b21de.zip |
Add ActiveRecord::Base#with_lock
Add a `with_lock` method to ActiveRecord objects, which starts
a transaction, locks the object (pessimistically) and yields to the block.
The method takes one (optional) parameter and passes it to `lock!`.
Before:
class Order < ActiveRecord::Base
def cancel!
transaction do
lock!
# ... cancelling logic
end
end
end
After:
class Order < ActiveRecord::Base
def cancel!
with_lock do
# ... cancelling logic
end
end
end
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/locking_test.rb | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb index 0c458d5318..807274ca67 100644 --- a/activerecord/test/cases/locking_test.rb +++ b/activerecord/test/cases/locking_test.rb @@ -388,6 +388,26 @@ unless current_adapter?(:SybaseAdapter, :OpenBaseAdapter) || in_memory_db? end end + def test_with_lock_commits_transaction + person = Person.find 1 + person.with_lock do + person.first_name = 'fooman' + person.save! + end + assert_equal 'fooman', person.reload.first_name + end + + def test_with_lock_rolls_back_transaction + person = Person.find 1 + old = person.first_name + person.with_lock do + person.first_name = 'fooman' + person.save! + raise 'oops' + end rescue nil + assert_equal old, person.reload.first_name + end + if current_adapter?(:PostgreSQLAdapter, :OracleAdapter) def test_no_locks_no_wait first, second = duel { Person.find 1 } |