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