Skip to content

Commit

Permalink
Fix: try to recover from idle/broken pg connection (take 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbaddaden committed Jan 12, 2016
1 parent 184642c commit b3c1586
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/database/postgresql.cr
Expand Up @@ -37,37 +37,35 @@ module Frost
end

def execute(sql)
log(sql)
__retry_on_connection_error { __execute(sql) }
end

def execute(types, sql)
log(sql)
__retry_on_connection_error { __execute(types, sql) }
end

private def __retry_on_connection_error
yield
rescue ex : PG::Error
if ex.message == "Connection not open"
begin
conn.reset
rescue err : PG::ConnectionError
raise ConnectionError.new(err.message)
end
yield
else
raise ex
begin
log("Rescued PG::Error: #{ex.message}")
conn.reset
rescue err : PG::ConnectionError
raise ConnectionError.new(err.message)
end

yield
end

private def __execute(sql)
log(sql)
conn.exec(sql)
rescue ex : PG::ResultError
raise StatementInvalid.new(ex.message)
end

private def __execute(types, sql)
log(sql)
conn.exec(types, sql)
rescue ex : PG::ResultError
raise StatementInvalid.new(ex.message)
Expand Down

0 comments on commit b3c1586

Please sign in to comment.