Class: ActiveRecord::ConnectionAdapters::TransactionManager

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ TransactionManager

Returns a new instance of TransactionManager.



191
192
193
194
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 191

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

Instance Method Details

#begin_transaction(options = {}) ⇒ Object



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

def begin_transaction(options = {})
  @connection.lock.synchronize do
    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}", @stack.last, options,
                                 run_commit_callbacks: run_commit_callbacks)
      end

    @stack.push(transaction)
    transaction
  end
end

#commit_transactionObject



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

def commit_transaction
  @connection.lock.synchronize do
    transaction = @stack.last

    begin
      transaction.before_commit_records
    ensure
      @stack.pop
    end

    transaction.commit
    transaction.commit_records
  end
end

#current_transactionObject



267
268
269
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 267

def current_transaction
  @stack.last || NULL_TRANSACTION
end

#open_transactionsObject



263
264
265
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 263

def open_transactions
  @stack.size
end

#rollback_transaction(transaction = nil) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 227

def rollback_transaction(transaction = nil)
  @connection.lock.synchronize do
    transaction ||= @stack.pop
    transaction.rollback
    transaction.rollback_records
  end
end

#within_new_transaction(options = {}) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 235

def within_new_transaction(options = {})
  @connection.lock.synchronize do
    begin
      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 if transaction
          rescue Exception
            rollback_transaction(transaction) unless transaction.state.completed?
            raise
          end
        end
      end
    end
  end
end