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.



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

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

Instance Method Details

#begin_transaction(options = {}) ⇒ Object



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

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



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

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



258
259
260
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 258

def current_transaction
  @stack.last || NULL_TRANSACTION
end

#open_transactionsObject



254
255
256
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 254

def open_transactions
  @stack.size
end

#rollback_transaction(transaction = nil) ⇒ Object



218
219
220
221
222
223
224
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 218

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

#within_new_transaction(options = {}) ⇒ Object



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

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