16

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?

Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157
  • Could you post your database.yml file? –  Jul 07 '15 at 17:20
  • 1
    You've tagged this with ruby-on-rails but I don't see any mention of Rails in the question, are you using it? If not, then why do you think database.yml is even being read? "For Rails 4.1+ you can set these values directly in your config/database.yml" https://devcenter.heroku.com/articles/concurrency-and-database-connections#threaded-servers Have a look at the linked Puma article and try setting up the pool in [on_worker_boot](https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot) – ian Jul 08 '15 at 03:49
  • I'm on Rails 4.2.1 and Postgres 9.4 – Jacek Kwiecień Jul 08 '15 at 07:21
  • I am confused. What are you using sinatra for? – froderik Jul 09 '15 at 11:38
  • It's a mobile app api – Jacek Kwiecień Jul 09 '15 at 19:23
  • Are you able to run stuff via the sql console? https://devcenter.heroku.com/articles/heroku-postgresql#pg-psql – ian Jul 10 '15 at 10:33
  • I think you head into the wrong direction I think it's about establishing and closing connection in the right time. Right now I do establish_connection every time the call to api is made, but no closing – Jacek Kwiecień Jul 11 '15 at 07:17
  • 2
    For maximum responsiveness, your application should maintain a constant connection to the database. To that end, ActiveRecord implements a *connection pool* that allows you to launch a bunch of connections and reuse them heavily. Try following Heroku's devcenter article first and you'll be able to catch the moment when everything breaks. Since there's no app's Ruby code, I don't think this question can be answered at all. – D-side Jul 13 '15 at 10:39

1 Answers1

4

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
Ojash
  • 1,234
  • 8
  • 20