Class: ActiveRecord::ConnectionAdapters::OpenTransaction

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

Overview

:nodoc:

Direct Known Subclasses

RealTransaction, SavepointTransaction

Instance Attribute Summary collapse

Attributes inherited from Transaction

#connection

Instance Method Summary collapse

Methods inherited from Transaction

#state

Constructor Details

#initialize(connection, parent, options = {}) ⇒ OpenTransaction

Returns a new instance of OpenTransaction.



72
73
74
75
76
77
78
79
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 72

def initialize(connection, parent, options = {})
  super connection

  @parent    = parent
  @records   = []
  @finishing = false
  @joinable  = options.fetch(:joinable, true)
end

Instance Attribute Details

#joinable=(value) ⇒ Object (writeonly)

Sets the attribute joinable

Parameters:

  • value

    the value to set the attribute joinable to.



70
71
72
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 70

def joinable=(value)
  @joinable = value
end

#parentObject (readonly)

Returns the value of attribute parent



69
70
71
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 69

def parent
  @parent
end

#recordsObject (readonly)

Returns the value of attribute records



69
70
71
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 69

def records
  @records
end

Instance Method Details

#add_record(record) ⇒ Object



120
121
122
123
124
125
126
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 120

def add_record(record)
  if record.has_transactional_callbacks?
    records << record
  else
    record.set_transaction_state(@state)
  end
end

#begin(options = {}) ⇒ Object



100
101
102
103
104
105
106
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 100

def begin(options = {})
  if finishing?
    parent.begin
  else
    SavepointTransaction.new(connection, self, options)
  end
end

#closed?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 150

def closed?
  false
end

#commitObject



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

def commit
  @finishing = true
  perform_commit
  parent
end

#commit_recordsObject



139
140
141
142
143
144
145
146
147
148
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 139

def commit_records
  @state.set_state(:committed)
  records.uniq.each do |record|
    begin
      record.committed!
    rescue => e
      record.logger.error(e) if record.respond_to?(:logger) && record.logger
    end
  end
end

#finishing?Boolean

This state is necessary so that we correctly handle stuff that might happen in a commit/rollback. But it’s kinda distasteful. Maybe we can find a better way to structure it in the future.

Returns:

  • (Boolean)


84
85
86
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 84

def finishing?
  @finishing
end

#joinable?Boolean

Returns:

  • (Boolean)


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

def joinable?
  @joinable && !finishing?
end

#numberObject



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

def number
  if finishing?
    parent.number
  else
    parent.number + 1
  end
end

#open?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 154

def open?
  true
end

#rollbackObject



108
109
110
111
112
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 108

def rollback
  @finishing = true
  perform_rollback
  parent
end

#rollback_recordsObject



128
129
130
131
132
133
134
135
136
137
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 128

def rollback_records
  @state.set_state(:rolledback)
  records.uniq.each do |record|
    begin
      record.rolledback!(parent.closed?)
    rescue => e
      record.logger.error(e) if record.respond_to?(:logger) && record.logger
    end
  end
end