On Mon, Jan 16, 2023 at 4:32 AM Artem S <[email protected]> wrote: > I have a rails application (with puma) and few postgresql databases. As a > DB driver gem Sequel is used. Here is the code for multiple DB connections > when application starting: > > require "sequel" > > module MyApp > class Databases > class << self > attr_accessor :first_db, :second_db > > def start_connections > @first_db = Sequel.connect(ENV.fetch("FIRST_DB_CREDENTIONALS")) > @second_db = Sequel.connect(ENV.fetch("SECOND_DB_CREDENTIONALS")) > end > > def disconnect_all > first_db.disconnect > second_db.disconnect > end > end > end > end > > MyApp::Databases.start_connections > > But, from time to time, some requests (which fetches records from first or > second DB) to application fails with error: "PG::ConnectionBad: > PQconsumeInput() server closed the connection unexpectedly. This probably > means the server terminated abnormally before or while processing the > request: ..." > > How to fix this error? Is there a problem with connection timeout settings > or what?
The best fix is to make sure your application has a consistent connection to the database. The database shouldn't be closing the connections, nor should any network equipment between the application and the database. Failing that, you could try the connection_validator extension, which can check that connections are valid before checking them out. This should handle most issues like the ones you are experiencing. One thing it won't help with is a connection becoming invalid after checkout, but hopefully that is an infrequent occurrence. Thanks, Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sequel-talk/CADGZSSfYTBN3Toygya8Q-owpwOtx-jDM42WJUipsHCqxhpOU%2BA%40mail.gmail.com.
