Class: ActiveRecord::ConnectionAdapters::Transaction

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

Overview

:nodoc:

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.



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

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
  @dirty = false
  @instrumenter = TransactionInstrumenter.new(connection: connection)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#isolation_levelObject (readonly)

Returns the value of attribute isolation_level.



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

def isolation_level
  @isolation_level
end

#savepoint_nameObject (readonly)

Returns the value of attribute savepoint_name.



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

def savepoint_name
  @savepoint_name
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

#writtenObject

Returns the value of attribute written.



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

def written
  @written
end

Instance Method Details

#add_record(record, ensure_finalize = true) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 147

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



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 208

def before_commit_records
  return unless records

  if @run_commit_callbacks
    if ActiveRecord.before_committed_on_all_records
      ite = unique_records

      instances_to_run_callbacks_on = records.each_with_object({}) do |record, candidates|
        candidates[record] = record
      end

      run_action_on_records(ite, instances_to_run_callbacks_on) do |record, should_run_callbacks|
        record.before_committed! if should_run_callbacks
      end
    else
      records.uniq.each(&:before_committed!)
    end
  end
end

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?; false; end

#commit_recordsObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 228

def commit_records
  return unless records

  ite = unique_records

  if @run_commit_callbacks
    instances_to_run_callbacks_on = prepare_instances_to_run_callbacks_on(ite)

    run_action_on_records(ite, instances_to_run_callbacks_on) do |record, should_run_callbacks|
      record.committed!(should_run_callbacks: should_run_callbacks)
    end
  else
    while record = ite.shift
      # 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

#dirty!Object



139
140
141
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 139

def dirty!
  @dirty = true
end

#dirty?Boolean

Returns:

  • (Boolean)


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

def dirty?
  @dirty
end

#full_rollback?Boolean

Returns:

  • (Boolean)


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

def full_rollback?; true; end

#incomplete!Object



171
172
173
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 171

def incomplete!
  @instrumenter.finish(:incomplete) if materialized?
end

#joinable?Boolean

Returns:

  • (Boolean)


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

def joinable?; @joinable; end

#materialize!Object



175
176
177
178
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 175

def materialize!
  @materialized = true
  @instrumenter.start
end

#materialized?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 180

def materialized?
  @materialized
end

#open?Boolean

Returns:

  • (Boolean)


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

def open?; !closed?; end

#recordsObject



157
158
159
160
161
162
163
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 157

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

#restartable?Boolean

Can this transaction’s current state be recreated by rollback+begin ?

Returns:

  • (Boolean)


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

def restartable?
  joinable? && !dirty?
end

#restore!Object



184
185
186
187
188
189
190
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 184

def restore!
  if materialized?
    incomplete!
    @materialized = false
    materialize!
  end
end

#rollback_recordsObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 192

def rollback_records
  return unless records

  ite = unique_records

  instances_to_run_callbacks_on = prepare_instances_to_run_callbacks_on(ite)

  run_action_on_records(ite, instances_to_run_callbacks_on) do |record, should_run_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