Class: ActiveRecord::ConnectionAdapters::Transaction

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

Overview

:nodoc:

Direct Known Subclasses

RealTransaction, SavepointTransaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, options, run_commit_callbacks: false) ⇒ Transaction

Returns a new instance of Transaction.


48
49
50
51
52
53
54
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 48

def initialize(connection, options, run_commit_callbacks: false)
  @connection = connection
  @state = TransactionState.new
  @records = []
  @joinable = options.fetch(:joinable, true)
  @run_commit_callbacks = run_commit_callbacks
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection


45
46
47
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 45

def connection
  @connection
end

#joinable=(value) ⇒ Object (writeonly)

Sets the attribute joinable

Parameters:

  • value

    the value to set the attribute joinable to.


46
47
48
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 46

def joinable=(value)
  @joinable = value
end

#recordsObject (readonly)

Returns the value of attribute records


45
46
47
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 45

def records
  @records
end

#savepoint_nameObject (readonly)

Returns the value of attribute savepoint_name


45
46
47
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 45

def savepoint_name
  @savepoint_name
end

#stateObject (readonly)

Returns the value of attribute state


45
46
47
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 45

def state
  @state
end

Instance Method Details

#add_record(record) ⇒ Object


56
57
58
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 56

def add_record(record)
  records << record
end

#before_commit_recordsObject


79
80
81
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 79

def before_commit_records
  records.uniq.each(&:before_committed!) if @run_commit_callbacks
end

#closed?Boolean

Returns:

  • (Boolean)

99
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 99

def closed?; false; end

#commitObject


75
76
77
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 75

def commit
  @state.set_state(:committed)
end

#commit_recordsObject


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 83

def commit_records
  ite = records.uniq
  while record = ite.shift
    if @run_commit_callbacks
      record.committed!
    else
      # if not running callbacks, only adds the record to the parent transaction
      record.add_to_transaction
    end
  end
ensure
  ite.each { |i| i.committed!(should_run_callbacks: false) }
end

#full_rollback?Boolean

Returns:

  • (Boolean)

97
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 97

def full_rollback?; true; end

#joinable?Boolean

Returns:

  • (Boolean)

98
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 98

def joinable?; @joinable; end

#open?Boolean

Returns:

  • (Boolean)

100
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 100

def open?; !closed?; end

#rollbackObject


60
61
62
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 60

def rollback
  @state.set_state(:rolledback)
end

#rollback_recordsObject


64
65
66
67
68
69
70
71
72
73
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 64

def rollback_records
  ite = records.uniq
  while record = ite.shift
    record.rolledback!(force_restore_state: full_rollback?)
  end
ensure
  ite.each do |i|
    i.rolledback!(force_restore_state: full_rollback?, should_run_callbacks: false)
  end
end