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

Returns a new instance of TransactionManager.


158
159
160
161
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 158

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

Instance Method Details

#begin_transaction(options = {}) ⇒ Object


163
164
165
166
167
168
169
170
171
172
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 163

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

#commit_transactionObject


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

def commit_transaction
  @stack.pop.commit
end

#current_transactionObject


207
208
209
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 207

def current_transaction
  @stack.last || NULL_TRANSACTION
end

#open_transactionsObject


203
204
205
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 203

def open_transactions
  @stack.size
end

#rollback_transactionObject


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

def rollback_transaction
  @stack.pop.rollback
end

#within_new_transaction(options = {}) ⇒ Object


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

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