diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-07-31 17:39:22 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-07-31 17:39:22 -0300 |
commit | c8317cd1d42c21fe33d38074841a9ffa28c7ed9b (patch) | |
tree | 1333fa5048656a9993f337fb2c781d727670ee8b /activerecord | |
parent | 88882964de8134c05355f8cfb303c4fc33a302bc (diff) | |
parent | 057c23715434adcab9b12f987d615979d1f57549 (diff) | |
download | rails-c8317cd1d42c21fe33d38074841a9ffa28c7ed9b.tar.gz rails-c8317cd1d42c21fe33d38074841a9ffa28c7ed9b.tar.bz2 rails-c8317cd1d42c21fe33d38074841a9ffa28c7ed9b.zip |
Merge pull request #16363 from arthurnn/null_transaction
Null transaction
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/transaction.rb | 135 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 2 |
2 files changed, 67 insertions, 70 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb index ff7f922d7f..46aaaae2ec 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb @@ -1,72 +1,5 @@ module ActiveRecord module ConnectionAdapters - class TransactionManager #:nodoc: - def initialize(connection) - @stack = [] - @connection = connection - end - - def begin_transaction(options = {}) - transaction_class = @stack.empty? ? RealTransaction : SavepointTransaction - transaction = transaction_class.new(@connection, "active_record_#{@stack.size}", options) - - @stack.push(transaction) - transaction - end - - def commit_transaction - @stack.pop.commit - end - - def rollback_transaction - @stack.pop.rollback - end - - def within_new_transaction(options = {}) - transaction = begin_transaction options - yield - rescue Exception => error - transaction.rollback if transaction - raise - ensure - begin - transaction.commit unless error - rescue Exception - transaction.rollback - raise - ensure - @stack.pop if transaction - end - end - - def open_transactions - @stack.size - end - - def current_transaction - @stack.last || closed_transaction - end - - private - - def closed_transaction - @closed_transaction ||= ClosedTransaction.new - end - end - - class Transaction #:nodoc: - attr_reader :connection, :state - - def initialize(connection) - @connection = connection - @state = TransactionState.new - end - - def savepoint_name - nil - end - end - class TransactionState attr_reader :parent @@ -97,8 +30,21 @@ module ActiveRecord end end - class ClosedTransaction < Transaction #:nodoc: - def initialize; super(nil); end + class Transaction #:nodoc: + attr_reader :connection, :state + + def initialize(connection) + @connection = connection + @state = TransactionState.new + end + + def savepoint_name + nil + end + end + + class NullTransaction < Transaction #:nodoc: + def initialize; end def closed?; true; end def open?; false; end def joinable?; false; end @@ -212,5 +158,56 @@ module ActiveRecord connection.release_savepoint(savepoint_name) end end + + class TransactionManager #:nodoc: + def initialize(connection) + @stack = [] + @connection = connection + end + + def begin_transaction(options = {}) + transaction_class = @stack.empty? ? RealTransaction : SavepointTransaction + transaction = transaction_class.new(@connection, "active_record_#{@stack.size}", options) + + @stack.push(transaction) + transaction + end + + def commit_transaction + @stack.pop.commit + end + + def rollback_transaction + @stack.pop.rollback + end + + def within_new_transaction(options = {}) + transaction = begin_transaction options + yield + rescue Exception => error + transaction.rollback if transaction + raise + ensure + begin + transaction.commit unless error + rescue Exception + transaction.rollback + raise + ensure + @stack.pop if transaction + end + end + + def open_transactions + @stack.size + end + + def current_transaction + @stack.last || NULL_TRANSACTION + end + + private + NULL_TRANSACTION = NullTransaction.new + end end end diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 99c728814a..a1b6671664 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -46,7 +46,7 @@ module ActiveRecord autoload_at 'active_record/connection_adapters/abstract/transaction' do autoload :TransactionManager - autoload :ClosedTransaction + autoload :NullTransaction autoload :RealTransaction autoload :SavepointTransaction autoload :TransactionState |