Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord::ConnectionNotEstablished - No connection pool for X

I can't make my sinatra/ruby app hosted on heroku works as desired. I fiddled with some setup trying to resolve this issue but so far no results.

ActiveRecord::ConnectionNotEstablished - No connection pool for User:
2015-06-25T14:26:11.736854+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:566:in `retrieve_connection'
2015-06-25T14:26:11.736856+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
2015-06-25T14:26:11.736858+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_handling.rb:87:in `connection'

User is one of my ActiveRecords table and the app fails because I try to query it.

I use sinatra with puma backup. Here is my Procfile:

web: ruby app/my-server.rb -s puma

I was also checking how many open connections there is using:

select count(*) from pg_stat_activity where pid <> pg_backend_pid()  and usename = current_user; 

but its says 0 every time.

I'm hosting the app on free plan and dev plan of herokupostgres.

I also noticed that the problem occurs when there are 2 quick calls to api at short interval of time. Like there was only 1, not 5 connections available, because 1st call succeds and the second one fails. In my database.yml I setup pool to 5.

I'm on Rails 4.2.1 and Postgres 9.4

Here is my database.yml aswell:

default: &default
  adapter: postgresql
  encoding: utf8
  pool: 5
  timeout: 5000

production:
  <<: *default
  host: my_db_address
  port: 5432
  database: my_db_name
  username: my_db_user_name
  password: my_db_password

< test and development ommited >

Do I miss some configuration or does free heroku plan chokes on it?

like image 249
Jacek Kwiecień Avatar asked Jun 25 '15 14:06

Jacek Kwiecień


1 Answers

Please check how your sinatra app established the connection

configure :production, :development do
  db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')
  pool = ENV["DB_POOL"] || ENV['MAX_THREADS'] || 5
  ActiveRecord::Base.establish_connection(
        adapter:  db.scheme == 'postgres' ? 'postgresql' : db.scheme,
        host:      db.host,
        username:  db.user,
        password:  db.password,
        database:  db.path[1..-1],
        encoding:  'utf8',
        pool:      pool
  )
end

Make sure you have proper settings for pool, also make sure you have a heroku config for DB_POOL or MAX_THREADS.

heroku config:set DB_POOL=5

or

heroku config:set MAX_THREADS=5
like image 70
Ojash Avatar answered Sep 26 '22 00:09

Ojash