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.



97
98
99
100
101
102
103
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 97

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.



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

def connection
  @connection
end

#joinable=(value) ⇒ Object (writeonly)

Sets the attribute joinable

Parameters:

  • value

    the value to set the attribute joinable to.



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

def joinable=(value)
  @joinable = value
end

#recordsObject (readonly)

Returns the value of attribute records.



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

def records
  @records
end

#savepoint_nameObject (readonly)

Returns the value of attribute savepoint_name.



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

def savepoint_name
  @savepoint_name
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

Instance Method Details

#add_record(record) ⇒ Object



105
106
107
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 105

def add_record(record)
  records << record
end

#before_commit_recordsObject



120
121
122
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 120

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

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?; false; end

#commit_recordsObject



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

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)


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

def full_rollback?; true; end

#joinable?Boolean

Returns:

  • (Boolean)


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

def joinable?; @joinable; end

#open?Boolean

Returns:

  • (Boolean)


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

def open?; !closed?; end

#rollback_recordsObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 109

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