Permalink
Browse files

Merge pull request #16363 from arthurnn/null_transaction

Null transaction
  • Loading branch information...
2 parents 8888296 + 057c237 commit c8317cd1d42c21fe33d38074841a9ffa28c7ed9b @rafaelfranca rafaelfranca committed Jul 31, 2014
@@ -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 @@ def set_state(state)
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 @@ def perform_commit
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
@@ -46,7 +46,7 @@ module ConnectionAdapters # :nodoc:
autoload_at 'active_record/connection_adapters/abstract/transaction' do
autoload :TransactionManager
- autoload :ClosedTransaction
+ autoload :NullTransaction
autoload :RealTransaction
autoload :SavepointTransaction
autoload :TransactionState

0 comments on commit c8317cd

Please sign in to comment.