diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2007-02-21 22:13:39 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2007-02-21 22:13:39 +0000 |
commit | 25bb98e42daa98c4207ea19bb80525d2b7cf8962 (patch) | |
tree | 72987b98040a64fe355540b780da0209ab2af0ae /activerecord/lib/active_record | |
parent | 7842caed942d5a410dcc0c22f2d3dfd808fa0cfa (diff) | |
download | rails-25bb98e42daa98c4207ea19bb80525d2b7cf8962.tar.gz rails-25bb98e42daa98c4207ea19bb80525d2b7cf8962.tar.bz2 rails-25bb98e42daa98c4207ea19bb80525d2b7cf8962.zip |
Added database connection as a yield parameter to ActiveRecord::Base.transaction so you can manually rollback [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6196 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb | 7 | ||||
-rw-r--r-- | activerecord/lib/active_record/transactions.rb | 11 |
2 files changed, 17 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index 7d7c22c74b..1b75ab53fe 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -56,7 +56,7 @@ module ActiveRecord begin_db_transaction transaction_open = true end - yield + yield self end rescue Exception => database_transaction_rollback if transaction_open @@ -79,6 +79,11 @@ module ActiveRecord # done if the transaction block raises an exception or returns false. def rollback_db_transaction() end + # Alias for rollback_db_transaction to be used when yielding the transaction + def rollback! + rollback_db_transaction + end + # Alias for #add_limit_offset!. def add_limit!(sql, options) add_limit_offset!(sql, options) if options diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index 77a8a14a2a..d9bfd331c6 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -32,6 +32,17 @@ module ActiveRecord # Exceptions will force a ROLLBACK that returns the database to the state before the transaction was begun. Be aware, though, # that the objects by default will _not_ have their instance data returned to their pre-transactional state. # + # == Rolling back a transaction manually + # + # Instead of relying on exceptions to rollback your transactions, you can also do so manually from within the scope + # of the transaction by accepting a yield parameter and calling rollback! on it. Example: + # + # transaction do |transaction| + # david.withdrawal(100) + # mary.deposit(100) + # transaction.rollback! # rolls back the transaction that was otherwise going to be successful + # end + # # == Transactions are not distributed across database connections # # A transaction acts on a single database connection. If you have |