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.


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

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


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

def connection
  @connection
end

#joinable=(value) ⇒ Object (writeonly)

Sets the attribute joinable

Parameters:

  • value

    the value to set the attribute joinable to.


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

def joinable=(value)
  @joinable = value
end

#recordsObject (readonly)

Returns the value of attribute records


77
78
79
# File '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 '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 'lib/active_record/connection_adapters/abstract/transaction.rb', line 77

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


111
112
113
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 111

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

#closed?Boolean

Returns:

  • (Boolean)

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

def closed?; false; end

#commitObject


107
108
109
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 107

def commit
  @state.commit!
end

#commit_recordsObject


115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 115

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)

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

def full_rollback?; true; end

#joinable?Boolean

Returns:

  • (Boolean)

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

def joinable?; @joinable; end

#open?Boolean

Returns:

  • (Boolean)

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

def open?; !closed?; end

#rollbackObject


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

def rollback
  @state.rollback!
end

#rollback_recordsObject


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

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