Class: ActiveRecord::ConnectionAdapters::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/abstract/transaction.rb

Overview

:nodoc:

Direct Known Subclasses

RealTransaction, SavepointTransaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, isolation: nil, joinable: true, run_commit_callbacks: false) ⇒ Transaction

Returns a new instance of Transaction.



89
90
91
92
93
94
95
96
97
98
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 89

def initialize(connection, isolation: nil, joinable: true, run_commit_callbacks: false)
  @connection = connection
  @state = TransactionState.new
  @records = nil
  @isolation_level = isolation
  @materialized = false
  @joinable = joinable
  @run_commit_callbacks = run_commit_callbacks
  @lazy_enrollment_records = nil
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



86
87
88
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 86

def connection
  @connection
end

#isolation_levelObject (readonly)

Returns the value of attribute isolation_level.



86
87
88
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 86

def isolation_level
  @isolation_level
end

#savepoint_nameObject (readonly)

Returns the value of attribute savepoint_name.



86
87
88
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 86

def savepoint_name
  @savepoint_name
end

#stateObject (readonly)

Returns the value of attribute state.



86
87
88
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 86

def state
  @state
end

#writtenObject

Returns the value of attribute written.



87
88
89
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 87

def written
  @written
end

Instance Method Details

#add_record(record, ensure_finalize = true) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 100

def add_record(record, ensure_finalize = true)
  @records ||= []
  if ensure_finalize
    @records << record
  else
    @lazy_enrollment_records ||= ObjectSpace::WeakMap.new
    @lazy_enrollment_records[record] = record
  end
end

#before_commit_recordsObject



142
143
144
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 142

def before_commit_records
  records.uniq.each(&:before_committed!) if records && @run_commit_callbacks
end

#closed?Boolean

Returns:

  • (Boolean)


167
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 167

def closed?; false; end

#commit_recordsObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 146

def commit_records
  return unless records
  ite = records.uniq(&:__id__)
  already_run_callbacks = {}
  while record = ite.shift
    if @run_commit_callbacks
      trigger_callbacks = record.trigger_transactional_callbacks?
      should_run_callbacks = !already_run_callbacks[record] && trigger_callbacks
      already_run_callbacks[record] ||= trigger_callbacks
      record.committed!(should_run_callbacks: should_run_callbacks)
    else
      # if not running callbacks, only adds the record to the parent transaction
      connection.add_transaction_record(record)
    end
  end
ensure
  ite&.each { |i| i.committed!(should_run_callbacks: false) }
end

#full_rollback?Boolean

Returns:

  • (Boolean)


165
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 165

def full_rollback?; true; end

#joinable?Boolean

Returns:

  • (Boolean)


166
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 166

def joinable?; @joinable; end

#materialize!Object



118
119
120
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 118

def materialize!
  @materialized = true
end

#materialized?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 122

def materialized?
  @materialized
end

#open?Boolean

Returns:

  • (Boolean)


168
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 168

def open?; !closed?; end

#recordsObject



110
111
112
113
114
115
116
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 110

def records
  if @lazy_enrollment_records
    @records.concat @lazy_enrollment_records.values
    @lazy_enrollment_records = nil
  end
  @records
end

#rollback_recordsObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 126

def rollback_records
  return unless records
  ite = records.uniq(&:__id__)
  already_run_callbacks = {}
  while record = ite.shift
    trigger_callbacks = record.trigger_transactional_callbacks?
    should_run_callbacks = !already_run_callbacks[record] && trigger_callbacks
    already_run_callbacks[record] ||= trigger_callbacks
    record.rolledback!(force_restore_state: full_rollback?, should_run_callbacks: should_run_callbacks)
  end
ensure
  ite&.each do |i|
    i.rolledback!(force_restore_state: full_rollback?, should_run_callbacks: false)
  end
end