Class: ActiveRecord::ConnectionAdapters::Transaction

Inherits:
Object
  • Object
show all
Defined in:
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.



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

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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#isolation_levelObject (readonly)

Returns the value of attribute isolation_level.



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

def isolation_level
  @isolation_level
end

#recordsObject (readonly)

Returns the value of attribute records.



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

def records
  @records
end

#savepoint_nameObject (readonly)

Returns the value of attribute savepoint_name.



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

def savepoint_name
  @savepoint_name
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

Instance Method Details

#add_record(record) ⇒ Object



88
89
90
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 88

def add_record(record)
  records << record
end

#before_commit_recordsObject



115
116
117
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 115

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

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?; false; end

#commit_recordsObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 119

def commit_records
  ite = records.uniq(&:__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)


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

def full_rollback?; true; end

#joinable?Boolean

Returns:

  • (Boolean)


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

def joinable?; @joinable; end

#materialize!Object



92
93
94
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 92

def materialize!
  @materialized = true
end

#materialized?Boolean

Returns:

  • (Boolean)


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

def materialized?
  @materialized
end

#open?Boolean

Returns:

  • (Boolean)


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

def open?; !closed?; end

#rollback_recordsObject



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

def rollback_records
  ite = records.uniq(&:__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