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) ⇒ Transaction

Returns a new instance of Transaction.



50
51
52
53
54
55
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 50

def initialize(connection, options)
  @connection = connection
  @state = TransactionState.new
  @records = []
  @joinable = options.fetch(:joinable, true)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#joinable=(value) ⇒ Object (writeonly)

Sets the attribute joinable

Parameters:

  • value

    the value to set the attribute joinable to.



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

def joinable=(value)
  @joinable = value
end

#recordsObject (readonly)

Returns the value of attribute records.



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

def records
  @records
end

#savepoint_nameObject (readonly)

Returns the value of attribute savepoint_name.



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

def savepoint_name
  @savepoint_name
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

Instance Method Details

#add_record(record) ⇒ Object



57
58
59
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 57

def add_record(record)
  records << record
end

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?; false; end

#commitObject



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

def commit
  @state.set_state(:committed)
end

#commit_recordsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 85

def commit_records
  ite = records.uniq
  while record = ite.shift
    begin
      record.committed!
    rescue => e
      raise if ActiveRecord::Base.raise_in_transactional_callbacks
      record.logger.error(e) if record.respond_to?(:logger) && record.logger
    end
  end
ensure
  ite.each do |i|
    i.committed!(false)
  end
end

#full_rollback?Boolean

Returns:

  • (Boolean)


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

def full_rollback?; true; end

#joinable?Boolean

Returns:

  • (Boolean)


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

def joinable?; @joinable; end

#open?Boolean

Returns:

  • (Boolean)


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

def open?; !closed?; end

#rollbackObject



61
62
63
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 61

def rollback
  @state.set_state(:rolledback)
end

#rollback_recordsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 65

def rollback_records
  ite = records.uniq
  while record = ite.shift
    begin
      record.rolledback! full_rollback?
    rescue => e
      raise if ActiveRecord::Base.raise_in_transactional_callbacks
      record.logger.error(e) if record.respond_to?(:logger) && record.logger
    end
  end
ensure
  ite.each do |i|
    i.rolledback!(full_rollback?, false)
  end
end