aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2015-03-02 11:55:18 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2015-03-02 11:55:18 -0800
commit07278519bb6db5579171fea70bccdfee1306f1d4 (patch)
treee37c40df15714fc3338d082239ddbf329b439e05 /activerecord
parent0814bb62e5128aff7bfa55211efbd10ad3e37c07 (diff)
downloadrails-07278519bb6db5579171fea70bccdfee1306f1d4.tar.gz
rails-07278519bb6db5579171fea70bccdfee1306f1d4.tar.bz2
rails-07278519bb6db5579171fea70bccdfee1306f1d4.zip
Revert "mutate the transaction object to reflect state"
This reverts commit 393e65b4170608593ad82377a9eadc918e85698d and ec51c3fedd16b561d096dcc1a6705fdc02ab7666 We don't want the records to hold hard references to transactions because they point at records that have callbacks.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/transaction.rb54
-rw-r--r--activerecord/lib/active_record/core.rb2
-rw-r--r--activerecord/lib/active_record/transactions.rb14
3 files changed, 33 insertions, 37 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
index 081ec1528a..c74f4e8fa5 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
@@ -1,30 +1,34 @@
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 @@ module ActiveRecord
def initialize(connection, options)
@connection = connection
- @state = TransactionState.null
+ @state = TransactionState.new
@records = []
@joinable = options.fetch(:joinable, true)
end
@@ -52,15 +56,7 @@ module ActiveRecord
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 @@ module ActiveRecord
end
def commit
- @state = TransactionState.committed
+ @state.set_state(:committed)
end
def before_commit_records
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index a89931b157..97ce4642aa 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -503,7 +503,7 @@ module ActiveRecord
@new_record = true
@txn = nil
@_start_transaction_state = {}
- @transaction = nil
+ @transaction_state = nil
end
def initialize_internals_callback
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb
index 283e1f8736..ffe7c4ae42 100644
--- a/activerecord/lib/active_record/transactions.rb
+++ b/activerecord/lib/active_record/transactions.rb
@@ -356,7 +356,7 @@ module ActiveRecord
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 @@ module ActiveRecord
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 @@ module ActiveRecord
# 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