Class: ActiveRecord::ConnectionAdapters::TransactionManager

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ TransactionManager


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

def initialize(connection)
  @stack = []
  @connection = connection
end

Instance Method Details

#begin_transaction(options = {}) ⇒ Object


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

def begin_transaction(options = {})
  run_commit_callbacks = !current_transaction.joinable?
  transaction =
    if @stack.empty?
      RealTransaction.new(@connection, options, run_commit_callbacks: run_commit_callbacks)
    else
      SavepointTransaction.new(@connection, "active_record_#{@stack.size}", options,
                               run_commit_callbacks: run_commit_callbacks)
    end

  @stack.push(transaction)
  transaction
end

#commit_transactionObject


168
169
170
171
172
173
174
175
176
177
178
179
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 168

def commit_transaction
  transaction = @stack.last

  begin
    transaction.before_commit_records
  ensure
    @stack.pop
  end

  transaction.commit
  transaction.commit_records
end

#current_transactionObject


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

def current_transaction
  @stack.last || NULL_TRANSACTION
end

#open_transactionsObject


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

def open_transactions
  @stack.size
end

#rollback_transaction(transaction = nil) ⇒ Object


181
182
183
184
185
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 181

def rollback_transaction(transaction = nil)
  transaction ||= @stack.pop
  transaction.rollback
  transaction.rollback_records
end

#within_new_transaction(options = {}) ⇒ Object


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

def within_new_transaction(options = {})
  transaction = begin_transaction options
  yield
rescue Exception => error
  if transaction
    rollback_transaction
    after_failure_actions(transaction, error)
  end
  raise
ensure
  unless error
    if Thread.current.status == 'aborting'
      rollback_transaction if transaction
    else
      begin
        commit_transaction
      rescue Exception
        rollback_transaction(transaction) unless transaction.state.completed?
        raise
      end
    end
  end
end