Class: ActiveRecord::ConnectionAdapters::Transaction

Inherits:
Object
  • Object
show all
Defined in:
activerecord/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.



96
97
98
99
100
101
102
103
104
105
106
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 96

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
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection



91
92
93
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 91

def connection
  @connection
end

#isolation_levelObject (readonly)

Returns the value of attribute isolation_level



91
92
93
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 91

def isolation_level
  @isolation_level
end

#savepoint_nameObject (readonly)

Returns the value of attribute savepoint_name



91
92
93
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 91

def savepoint_name
  @savepoint_name
end

#stateObject (readonly)

Returns the value of attribute state



91
92
93
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 91

def state
  @state
end

#writtenObject

Returns the value of attribute written



92
93
94
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 92

def written
  @written
end

#written_indirectlyObject

Returns the value of attribute written_indirectly



92
93
94
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 92

def written_indirectly
  @written_indirectly
end

Instance Method Details

#add_record(record, ensure_finalize = true) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 116

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



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 171

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)


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

def closed?; false; end

#commit_recordsObject



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

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



108
109
110
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 108

def dirty!
  @dirty = true
end

#dirty?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 112

def dirty?
  @dirty
end

#full_rollback?Boolean

Returns:

  • (Boolean)


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

def full_rollback?; true; end

#joinable?Boolean

Returns:

  • (Boolean)


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

def joinable?; @joinable; end

#materialize!Object



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

def materialize!
  @materialized = true
end

#materialized?Boolean

Returns:

  • (Boolean)


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

def materialized?
  @materialized
end

#open?Boolean

Returns:

  • (Boolean)


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

def open?; !closed?; end

#recordsObject



126
127
128
129
130
131
132
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 126

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)


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

def restartable?
  joinable? && !dirty?
end

#restore!Object



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

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

#rollback_recordsObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 155

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