ActiveRecord::ConnectionNotEstablished - X没有连接池

16
我无法使我的sinatra/ruby应用在heroku上按照预期工作。我尝试了一些设置来解决这个问题,但目前还没有结果。
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'

用户是我的ActiveRecords表之一,应用程序失败是因为我尝试查询它。

我使用的是带有Puma备份的Sinatra。以下是我的Procfile:

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

我还检查了使用以下方法有多少个打开的连接:

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

但它每次都显示为0。

我正在使用Heroku Postgres的免费计划和开发计划托管应用程序。

我还注意到当在短时间内进行两个快速的API调用时,问题就会出现。就像只有1个连接可用,而不是5个,因为第一个调用成功了,而第二个调用失败了。在我的database.yml中,我将池设置为5。

我使用Rails 4.2.1和Postgres 9.4。

这是我的database.yml文件:

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 >

我是不是错过了某些配置,还是免费的Heroku计划对此无能为力?


你能发一下你的database.yml文件吗? - user3720516
1
您已经使用ruby-on-rails标记了这个问题,但我没有看到问题中提到Rails,您是否在使用它?如果不是,请问您认为database.yml甚至被读取的原因是什么? "对于Rails 4.1+,您可以直接在config/database.yml中设置这些值" https://devcenter.heroku.com/articles/concurrency-and-database-connections#threaded-servers 请查看链接的Puma文章,并尝试在on_worker_boot中设置池。 - ian
这是一个移动应用程序API。 - Jacek Kwiecień
你能通过 SQL 控制台运行东西吗?https://devcenter.heroku.com/articles/heroku-postgresql#pg-psql - ian
2
为了最大限度地提高响应能力,您的应用程序应该与数据库保持恒定的连接。为此,ActiveRecord 实现了一个连接池,允许您启动一堆连接并重复使用它们。首先尝试遵循 Heroku 的 devcenter 文章,您将能够捕捉到一切崩溃的时刻。由于没有应用程序的 Ruby 代码,我认为这个问题根本无法回答。 - D-side
显示剩余3条评论
1个回答

4
请检查您的Sinatra应用程序如何建立连接。
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

确保您的池设置正确,还要确保您在Heroku配置中设置了DB_POOLMAX_THREADS

heroku config:set DB_POOL=5

或者

heroku config:set MAX_THREADS=5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接