Module: StatementTimeout::QueryMethodsExtension

Defined in:
lib/statement_timeout.rb

Instance Method Summary collapse

Instance Method Details

#statement_timeout(timeout) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/statement_timeout.rb', line 29

def statement_timeout(timeout)
  timeout = if timeout in ActiveSupport::Duration
              timeout.in_milliseconds
            else
              timeout
            end

  connection_pool.with_connection do |connection|
    raise ActiveRecord::AdapterError, "statement_timeout is not supported for the #{connection.class.inspect} adapter" unless
      connection.supports_statement_timeout?

    statement_timeout_was, connection.statement_timeout = connection.statement_timeout, timeout

    yield connection
  ensure
    connection.statement_timeout = statement_timeout_was
  end
end