提交 07278519 编写于 作者: A Aaron Patterson

Revert "mutate the transaction object to reflect state"

This reverts commit 393e65b4 and
ec51c3fe

We don't want the records to hold hard references to transactions
because they point at records that have callbacks.
上级 0814bb62
module ActiveRecord
module ConnectionAdapters
class TransactionState
def finalized?; false; end
def committed?; false; end
def rolledback?; false; end
def completed?; false; end
VALID_STATES = Set.new([:committed, :rolledback, nil])
class Committed < TransactionState
def finalized?; true; end
def committed?; true; end
def completed?; true; end
def initialize(state = nil)
@state = state
end
class RolledBack < TransactionState
def finalized?; true; end
def rolledback?; true; end
def completed?; true; end
def finalized?
@state
end
def committed?
@state == :committed
end
def rolledback?
@state == :rolledback
end
ROLLED_BACK = RolledBack.new
COMMITTED = Committed.new
NULL = TransactionState.new
def completed?
committed? || rolledback?
end
def self.rolled_back; ROLLED_BACK; end
def self.committed; COMMITTED; end
def self.null; NULL; end
def set_state(state)
unless VALID_STATES.include?(state)
raise ArgumentError, "Invalid transaction state: #{state}"
end
@state = state
end
end
class NullTransaction #:nodoc:
......@@ -42,7 +46,7 @@ class Transaction #:nodoc:
def initialize(connection, options)
@connection = connection
@state = TransactionState.null
@state = TransactionState.new
@records = []
@joinable = options.fetch(:joinable, true)
end
......@@ -52,15 +56,7 @@ def add_record(record)
end
def rollback
@state = TransactionState.rolled_back
end
def rolledback?
@state.rolledback?
end
def finalized?
@state.finalized?
@state.set_state(:rolledback)
end
def rollback_records
......@@ -75,7 +71,7 @@ def rollback_records
end
def commit
@state = TransactionState.committed
@state.set_state(:committed)
end
def before_commit_records
......
......@@ -503,7 +503,7 @@ def init_internals
@new_record = true
@txn = nil
@_start_transaction_state = {}
@transaction = nil
@transaction_state = nil
end
def initialize_internals_callback
......
......@@ -356,7 +356,7 @@ def add_to_transaction
if has_transactional_callbacks?
self.class.connection.add_transaction_record(self)
else
set_transaction(self.class.connection.current_transaction)
set_transaction_state(self.class.connection.transaction_state)
end
remember_transaction_record_state
end
......@@ -445,8 +445,8 @@ def transaction_include_any_action?(actions) #:nodoc:
private
def set_transaction(txn) # :nodoc:
@transaction = txn
def set_transaction_state(state) # :nodoc:
@transaction_state = state
end
def has_transactional_callbacks? # :nodoc:
......@@ -471,12 +471,12 @@ def has_transactional_callbacks? # :nodoc:
# method recursively goes through the parent of the TransactionState and
# checks if the ActiveRecord object reflects the state of the object.
def sync_with_transaction_state
update_attributes_from_transaction_state(@transaction)
update_attributes_from_transaction_state(@transaction_state)
end
def update_attributes_from_transaction_state(transaction)
if transaction && transaction.finalized?
restore_transaction_record_state if transaction.rolledback?
def update_attributes_from_transaction_state(transaction_state)
if transaction_state && transaction_state.finalized?
restore_transaction_record_state if transaction_state.rolledback?
clear_transaction_record_state
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册