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



24
25
26
27
28
29
30
31
32
33
# File 'lib/active_record_query_counter/transaction_extension.rb', line 24

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
20
21
22
# 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)
  @active_record_query_counter_gc_start = GC.total_time
  @active_record_query_counter_cpu_start = ActiveRecordQueryCounter.current_cpu_time
  @active_record_query_counter_queries = ActiveRecordQueryCounter.register_transaction_queries(connection)
end

#rollbackObject



35
36
37
38
39
40
41
# File 'lib/active_record_query_counter/transaction_extension.rb', line 35

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