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.



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 120

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.



115
116
117
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 115

def connection
  @connection
end

#isolation_levelObject (readonly)

Returns the value of attribute isolation_level.



115
116
117
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 115

def isolation_level
  @isolation_level
end

#savepoint_nameObject (readonly)

Returns the value of attribute savepoint_name.



115
116
117
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 115

def savepoint_name
  @savepoint_name
end

#stateObject (readonly)

Returns the value of attribute state.



115
116
117
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 115

def state
  @state
end

#writtenObject

Returns the value of attribute written.



116
117
118
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 116

def written
  @written
end

Instance Method Details

#add_record(record, ensure_finalize = true) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 141

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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 201

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)


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

def closed?; false; end

#commit_recordsObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 221

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



133
134
135
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 133

def dirty!
  @dirty = true
end

#dirty?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 137

def dirty?
  @dirty
end

#full_rollback?Boolean

Returns:

  • (Boolean)


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

def full_rollback?; true; end

#incomplete!Object



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

def incomplete!
  @instrumenter.finish
end

#joinable?Boolean

Returns:

  • (Boolean)


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

def joinable?; @joinable; end

#materialize!Object



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

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

#materialized?Boolean

Returns:

  • (Boolean)


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

def materialized?
  @materialized
end

#open?Boolean

Returns:

  • (Boolean)


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

def open?; !closed?; end

#recordsObject



151
152
153
154
155
156
157
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 151

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)


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

def restartable?
  joinable? && !dirty?
end

#restore!Object



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

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

#rollback_recordsObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 185

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