After a database downtime, Rails will first throw this error once:
ActiveRecord::StatementInvalid: NativeException: org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
From then on, every database call with have the following error, even after the database is back up:
ActiveRecord::StatementInvalid: ActiveRecord::JDBCError: This connection has been closed.
To get the server running again, I have to restart the rails server. This is not ideal for us, as our prod engineers would like to do maintenance on our databases without having to also bring back up all the services that depend on the database. So, I'm wondering - is there a way to automatically get Rails to try to reestablish the database connection or a recommended way to get this behavior?
Things I have tried:
I have already tried setting reconnect to true in my database options, and with that, I can kill individual database connections, and rails will reestablish the connections. However, it will not after a database outage. I found that from a command console I could get the connection back up by calling
ActiveRecord::Base::establish_connection
So maybe finding a clean place for rails to call the above command this would work? Any suggestions?