Heroku Rails - Disable Active Record Postgres Connection

I have a rails 4 application that I place on a hero. They give some specific recommendations on how to manage the database connection pool when using a multi-threaded server (puma) https://devcenter.heroku.com/articles/concurrency-and-database-connections

When I started load testing for my application, I got an error - I can not connect to db. when each page hits, the rails initialize the active record, even if I do not make any requests on this page or refer to any models.

My question is:

How can I make some kind of whitelist (or blacklist) so that the active record is not initialized with a db connection for these specific controller actions? In the initializer?

Ideally, I would run a cheaper postgres service on heroku (40 connections) because I know that my application does not use db very often. If traffic gets higher than 40 connections, an error will occur, which seems silly for an application that was not going to use db for these requests.

I read about how to disable active recording for the entire application: Disable ActiveRecord for Rails 4

But how to selectively enable it? Are there any other performance considerations here (not loading these things or any other errors)

+8
ruby-on-rails activerecord postgresql connection-pooling heroku
source share
1 answer

In you application_controller.rb

 before_filter :maybe_disconnect_db def maybe_disconnect_db ActiveRecord::Base.remove_connection() if ActiveRecord::Base.connected? end def maybe_connect_db ActiveRecord::Base.establish_connection() unless ActiveRecord::Base.connected? end 

Then for each controller / action that needs a db connection, add

 skip_before_filter :maybe_disconnect_db, #only or unless filter here before_filter :maybe_connect_db, #only or unless filter here 

This should establish a connection for any specific db request and disconnect for any request that it does not need, and also process several db requests in a row without action and several non db requests in a line without any action.

ActiveRecord :: Base.remove_connection

ActiveRecord :: Base.establish_connection

0
source share

All Articles