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.



47
48
49
50
51
52
53
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 47

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.



44
45
46
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 44

def connection
  @connection
end

#joinable=(value) ⇒ Object (writeonly)

Sets the attribute joinable

Parameters:

  • value

    the value to set the attribute joinable to.



45
46
47
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 45

def joinable=(value)
  @joinable = value
end

#recordsObject (readonly)

Returns the value of attribute records.



44
45
46
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 44

def records
  @records
end

#savepoint_nameObject (readonly)

Returns the value of attribute savepoint_name.



44
45
46
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 44

def savepoint_name
  @savepoint_name
end

#stateObject (readonly)

Returns the value of attribute state.



44
45
46
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 44

def state
  @state
end

Instance Method Details

#add_record(record) ⇒ Object



55
56
57
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 55

def add_record(record)
  records << record
end

#before_commit_recordsObject



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

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

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?; false; end

#commitObject



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

def commit
  @state.set_state(:committed)
end

#commit_recordsObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 82

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)


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

def full_rollback?; true; end

#joinable?Boolean

Returns:

  • (Boolean)


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

def joinable?; @joinable; end

#open?Boolean

Returns:

  • (Boolean)


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

def open?; !closed?; end

#rollbackObject



59
60
61
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 59

def rollback
  @state.set_state(:rolledback)
end

#rollback_recordsObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 63

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