Class: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_reconnect/ar23.rb,
lib/pg_reconnect/ar3.rb

Constant Summary collapse

MAX_QUERY_RETRIES =
1

Instance Method Summary collapse

Instance Method Details

#exec_query(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pg_reconnect/ar3.rb', line 7

def exec_query(*args)
  res = exec_query_without_retry(*args)
  @retry_counter = nil
  res
rescue ActiveRecord::StatementInvalid => ex
  unless active?        
    @retry_counter ||= 0
    send(:log, Logger::WARN) { "Reconnecting to database after PGError! Try ##{@retry_counter + 1}/#{MAX_QUERY_RETRIES} #{ex.message}, trace: #{ex.backtrace.inspect}" }

    if @retry_counter < MAX_QUERY_RETRIES
      @retry_counter += 1
      reconnect! rescue nil
      retry
    end
  end
  
  @retry_counter = 0
  raise
end

#exec_query_without_retryObject



5
# File 'lib/pg_reconnect/ar3.rb', line 5

alias_method :exec_query_without_retry, :exec_query

#execute(*args) ⇒ Object



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

def execute(*args)
  res = execute_without_retry(*args)
  @retry_counter = nil
  res
rescue ActiveRecord::StatementInvalid => ex
  unless active?
    @retry_counter ||= 0
    @logger.warn "Reconnecting to database after PGError! Try ##{@retry_counter + 1}/#{MAX_QUERY_RETRIES} #{ex.message}, trace: #{ex.backtrace.inspect}"

    if @retry_counter < MAX_QUERY_RETRIES
      @retry_counter += 1
      reconnect! rescue nil # to prevent error PG::Error: invalid encoding name: utf8
      retry
    end
  end

  @retry_counter = 0    
  raise
end

#execute_without_retryObject



5
# File 'lib/pg_reconnect/ar23.rb', line 5

alias_method :execute_without_retry, :execute

#query(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pg_reconnect/ar23.rb', line 8

def query(*args)
  res = query_without_retry(*args)
  @retry_counter = nil
  res
rescue ActiveRecord::StatementInvalid => ex
  unless active?     
    @retry_counter ||= 0
    @logger.warn "Reconnecting to database after PGError! Try ##{@retry_counter + 1}/#{MAX_QUERY_RETRIES} #{ex.message}, trace: #{ex.backtrace.inspect}"

    if @retry_counter < MAX_QUERY_RETRIES
      @retry_counter += 1
      reconnect! rescue nil # to prevent error PG::Error: invalid encoding name: utf8
      retry
    end
  end

  @retry_counter = 0
  raise
end

#query_without_retryObject



6
# File 'lib/pg_reconnect/ar23.rb', line 6

alias_method :query_without_retry, :query