4


my problem is a pgbouncer which is dropping connections. Sysadmin set the client_idle_timeout to 60 seconds. Is there any solve on rails side for this? I mean, is there a possibility (a gem, or settings) to connect and drop connection (reconnection) to database every query we want to send ?

ruby 2.0.0
rails 4.2.3
pg 0.17.1

Im getting following error when connection is longer than x seconds (for example when I open rails console, wait x seconds and then run some ActiveRecord query to db):

ERROR: client_idle_timeout
Contract Load (0.5ms) SELECT "contracts".* FROM "contracts" ORDER BY "contracts"."id" ASC LIMIT 1
PG::ConnectionBad: PQconsumeInput() SSL connection has been closed unexpectedly :
SELECT "contracts".* FROM "contracts" ORDER BY "contracts"."id" ASC LIMIT 1
ActiveRecord::StatementInvalid: PG::ConnectionBad: PQconsumeInput() SSL connection has been closed unexpectedly :
SELECT "contracts".* FROM "contracts" ORDER BY "contracts"."id" ASC LIMIT 1

  • Yeah, I ran into a similar issue, the problem is the idle timeout configured, you have to increase it otherwise you cannot do anything in the console. You can do a huge try/catch when rails executes the queries and tries to reconnect with active record manually after the connection is gone. – MaicolBen Mar 20 '19 at 13:26
  • @MaicolBen its not a fix. About those two I knew. I was looking for a fix for this than a runaround. Like for example reconnecting every time this happens or something. – mbruszewski Mar 21 '19 at 14:16

1 Answers1

2

Calling ActiveRecord::Base.clear_active_connections! will return the stale connections back into the thread pool and check out fresh connections so you don't have to restart your console/server