Module: ActiveRecordQueryCounter::TransactionExtension

Defined in:
lib/active_record_query_counter/transaction_extension.rb

Overview

Extension to ActiveRecord::ConnectionAdapters::RealTransaction to count transactions. Real transactions are always the outermost database transaction; savepoint transactions nested inside them are considered part of the transaction and are not counted separately.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inject(transaction_class) ⇒ Object



9
10
11
12
13
# File 'lib/active_record_query_counter/transaction_extension.rb', line 9

def inject(transaction_class)
  unless transaction_class.include?(self)
    transaction_class.prepend(self)
  end
end

Instance Method Details

#commitObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/active_record_query_counter/transaction_extension.rb', line 21

def commit(...)
  # The transaction is only recorded after the COMMIT succeeds. If it raises, the start
  # time is left in place so the rollback that Rails performs next is counted as a
  # rollback rather than a successful commit. Recording here rather than in the
  # transaction manager keeps time spent in after commit callbacks (which run after this
  # method returns) out of the transaction time.
  retval = super
  active_record_query_counter_record_transaction
  retval
end

#initializeObject



16
17
18
19
# File 'lib/active_record_query_counter/transaction_extension.rb', line 16

def initialize(...)
  super
  @active_record_query_counter_start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

#rollbackObject



32
33
34
35
36
37
38
# File 'lib/active_record_query_counter/transaction_extension.rb', line 32

def rollback(...)
  super
ensure
  # Recorded even if the ROLLBACK itself raises (e.g. the connection died) since the
  # transaction is over either way.
  active_record_query_counter_record_transaction(rollback: true)
end