Class: EM::Synchrony::ActiveRecord::Adapter_4_2::TransactionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/fibered_mysql2/fibered_mysql2_connection_factory.rb

Instance Method Summary collapse

Instance Method Details

#begin_transaction(options = {}) ⇒ Object

Overriding the em-synchrony override to bring it up to rails 6 requirements. Changes from the original Rails 6 source are:

1. the usage of _current_stack created by em-synchrony instead of the Rails provided @stack instance variable
2. the usage of Fiber.current.object_id as a part of the savepoint transaction name

Original EM Synchrony Source: github.com/igrigorik/em-synchrony/blob/master/lib/em-synchrony/activerecord_4_2.rb#L35-L44

Original Rails Source: github.com/rails/rails/blob/6-0-stable/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb#L205-L224



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fibered_mysql2/fibered_mysql2_connection_factory.rb', line 30

def begin_transaction(options = {})
  @connection.lock.synchronize do
    run_commit_callbacks = !current_transaction.joinable?
    transaction =
        if _current_stack.empty?
          ::ActiveRecord::ConnectionAdapters::RealTransaction.new(@connection, options, run_commit_callbacks: run_commit_callbacks)
        else
          ::ActiveRecord::ConnectionAdapters::SavepointTransaction.new(@connection, "active_record_#{Fiber.current.object_id}_#{open_transactions}", _current_stack.last, options,
                                                                       run_commit_callbacks: run_commit_callbacks)
        end

    if @connection.supports_lazy_transactions? && lazy_transactions_enabled? && options[:_lazy] != false
      @has_unmaterialized_transactions = true
    else
      transaction.materialize!
    end
    _current_stack.push(transaction)
    transaction
  end
end

#commit_transactionObject

Overriding the ActiveRecord::TransactionManager#commit_transaction method to use fiber safe the _current_stack instead of the @stack instance variable. when marterializing transactions.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fibered_mysql2/fibered_mysql2_connection_factory.rb', line 72

def commit_transaction
  @connection.lock.synchronize do
    transaction = _current_stack.last

    begin
      transaction.before_commit_records
    ensure
      _current_stack.pop
    end

    transaction.commit
    transaction.commit_records
  end
end

#materialize_transactionsObject

Overriding the ActiveRecord::TransactionManager#materialize_transactions method to use fiber safe the _current_stack instead of the @stack instance variable. when marterializing transactions.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fibered_mysql2/fibered_mysql2_connection_factory.rb', line 54

def materialize_transactions
  return if @materializing_transactions
  return unless @has_unmaterialized_transactions

  @connection.lock.synchronize do
    begin
      @materializing_transactions = true
      _current_stack.each { |t| t.materialize! unless t.materialized? }
    ensure
      @materializing_transactions = false
    end
    @has_unmaterialized_transactions = false
  end
end

#rollback_transaction(transaction = nil) ⇒ Object

Overriding the ActiveRecord::TransactionManager#rollback_transaction method to use fiber safe the _current_stack instead of the @stack instance variable. when marterializing transactions.



90
91
92
93
94
95
96
# File 'lib/fibered_mysql2/fibered_mysql2_connection_factory.rb', line 90

def rollback_transaction(transaction = nil)
  @connection.lock.synchronize do
    transaction ||= _current_stack.pop
    transaction.rollback
    transaction.rollback_records
  end
end