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.


76
77
78
79
80
81
82
83
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 76

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.


74
75
76
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 74

def joinable=(value)
  @joinable = value
end

#parentObject (readonly)

Returns the value of attribute parent


73
74
75
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 73

def parent
  @parent
end

#recordsObject (readonly)

Returns the value of attribute records


73
74
75
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 73

def records
  @records
end

Instance Method Details

#add_record(record) ⇒ Object


124
125
126
127
128
129
130
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 124

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

#begin(options = {}) ⇒ Object


104
105
106
107
108
109
110
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 104

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

#closed?Boolean

Returns:

  • (Boolean)

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

def closed?
  false
end

#commitObject


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

def commit
  @finishing = true
  perform_commit
  parent
end

#commit_recordsObject


143
144
145
146
147
148
149
150
151
152
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 143

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)

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

def finishing?
  @finishing
end

#joinable?Boolean

Returns:

  • (Boolean)

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

def joinable?
  @joinable && !finishing?
end

#numberObject


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

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

#open?Boolean

Returns:

  • (Boolean)

158
159
160
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 158

def open?
  true
end

#rollbackObject


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

def rollback
  @finishing = true
  perform_rollback
  parent
end

#rollback_recordsObject


132
133
134
135
136
137
138
139
140
141
# File 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb', line 132

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