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, isolation: nil, joinable: true, run_commit_callbacks: false) ⇒ Transaction

Returns a new instance of Transaction.



79
80
81
82
83
84
85
86
87
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 79

def initialize(connection, isolation: nil, joinable: true, run_commit_callbacks: false)
  @connection = connection
  @state = TransactionState.new
  @records = nil
  @isolation_level = isolation
  @materialized = false
  @joinable = joinable
  @run_commit_callbacks = run_commit_callbacks
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection



77
78
79
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 77

def connection
  @connection
end

#isolation_levelObject (readonly)

Returns the value of attribute isolation_level



77
78
79
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 77

def isolation_level
  @isolation_level
end

#recordsObject (readonly)

Returns the value of attribute records



77
78
79
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 77

def records
  @records
end

#savepoint_nameObject (readonly)

Returns the value of attribute savepoint_name



77
78
79
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 77

def savepoint_name
  @savepoint_name
end

#stateObject (readonly)

Returns the value of attribute state



77
78
79
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 77

def state
  @state
end

Instance Method Details

#add_record(record) ⇒ Object



89
90
91
92
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 89

def add_record(record)
  @records ||= []
  @records << record
end

#before_commit_recordsObject



118
119
120
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 118

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

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?; false; end

#commit_recordsObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 122

def commit_records
  return unless records
  ite = records.uniq(&:object_id)
  already_run_callbacks = {}
  while record = ite.shift
    if @run_commit_callbacks
      trigger_callbacks = record.trigger_transactional_callbacks?
      should_run_callbacks = !already_run_callbacks[record] && trigger_callbacks
      already_run_callbacks[record] ||= trigger_callbacks
      record.committed!(should_run_callbacks: should_run_callbacks)
    else
      # if not running callbacks, only adds the record to the parent transaction
      connection.add_transaction_record(record)
    end
  end
ensure
  ite&.each { |i| i.committed!(should_run_callbacks: false) }
end

#full_rollback?Boolean

Returns:

  • (Boolean)


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

def full_rollback?; true; end

#joinable?Boolean

Returns:

  • (Boolean)


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

def joinable?; @joinable; end

#materialize!Object



94
95
96
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 94

def materialize!
  @materialized = true
end

#materialized?Boolean

Returns:

  • (Boolean)


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

def materialized?
  @materialized
end

#open?Boolean

Returns:

  • (Boolean)


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

def open?; !closed?; end

#rollback_recordsObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 102

def rollback_records
  return unless records
  ite = records.uniq(&:object_id)
  already_run_callbacks = {}
  while record = ite.shift
    trigger_callbacks = record.trigger_transactional_callbacks?
    should_run_callbacks = !already_run_callbacks[record] && trigger_callbacks
    already_run_callbacks[record] ||= trigger_callbacks
    record.rolledback!(force_restore_state: full_rollback?, should_run_callbacks: should_run_callbacks)
  end
ensure
  ite&.each do |i|
    i.rolledback!(force_restore_state: full_rollback?, should_run_callbacks: false)
  end
end