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.



197
198
199
200
201
202
203
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 197

def initialize(connection)
  @stack = []
  @connection = connection
  @has_unmaterialized_transactions = false
  @materializing_transactions = false
  @lazy_transactions_enabled = true
end

Instance Method Details

#begin_transaction(options = {}) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 205

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

    if @connection.supports_lazy_transactions? && lazy_transactions_enabled? && options[:_lazy] != false
      @has_unmaterialized_transactions = true
    else
      transaction.materialize!
    end
    @stack.push(transaction)
    transaction
  end
end

#commit_transactionObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 254

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



307
308
309
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 307

def current_transaction
  @stack.last || NULL_TRANSACTION
end

#disable_lazy_transactions!Object



226
227
228
229
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 226

def disable_lazy_transactions!
  materialize_transactions
  @lazy_transactions_enabled = false
end

#enable_lazy_transactions!Object



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

def enable_lazy_transactions!
  @lazy_transactions_enabled = true
end

#lazy_transactions_enabled?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 235

def lazy_transactions_enabled?
  @lazy_transactions_enabled
end

#materialize_transactionsObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 239

def materialize_transactions
  return if @materializing_transactions
  return unless @has_unmaterialized_transactions

  @connection.lock.synchronize do
    begin
      @materializing_transactions = true
      @stack.each { |t| t.materialize! unless t.materialized? }
    ensure
      @materializing_transactions = false
    end
    @has_unmaterialized_transactions = false
  end
end

#open_transactionsObject



303
304
305
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 303

def open_transactions
  @stack.size
end

#rollback_transaction(transaction = nil) ⇒ Object



269
270
271
272
273
274
275
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 269

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

#within_new_transaction(options = {}) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 277

def within_new_transaction(options = {})
  @connection.lock.synchronize do
    transaction = begin_transaction options
    yield
  rescue Exception => error
    if transaction
      rollback_transaction
      after_failure_actions(transaction, error)
    end
    raise
  ensure
    if !error && transaction
      if Thread.current.status == "aborting"
        rollback_transaction
      else
        begin
          commit_transaction
        rescue Exception
          rollback_transaction(transaction) unless transaction.state.completed?
          raise
        end
      end
    end
  end
end