ActiveRecord :: ConnectionTimeoutError:无法在5.000秒内从池中获取连接。所有汇集的连接都在使用中

时间:2017-05-21 16:10:52

标签: ruby-on-rails ruby activerecord activeresource

我正在尝试在单轨应用程序中使用多个数据库。我创建了一个模型ReportUser来访问在config / support_database.yml中指定的不同数据库中定义的report_users表。正常模型activeRecord查询正在运行,但ReportUser.count不起作用。我设法创建表和迁移,但是当我尝试使用活动记录查询访问辅助数据库模型时,我从第二个数据库获得连接超时错误。

以下是我所遵循的一些事情。

创建了不同的config / database_support.yml文件

default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: sm_development

test:
  <<: *default
  database: sm_test

配置/初始化/ general.rb

SUPPORT_DB = YAML.load_file(File.join(Rails.root, "config", "database_support.yml"))[Rails.env.to_s]

应用程序/模型/ support_base.rb

class SupportBase < ActiveRecord::Base 
  self.abstract_class = true 
  establish_connection(SUPPORT_DB)
end

应用程序/模型/ report_user.rb

class ReportUser < SupportBase
end

db_support /迁移/ 2017_create_report_users.rb

class CreateReportUsers < ActiveRecord::Migration[5.0]
  def change
    create_table :report_users do |t|
      t.string :first_name
      t.string :last_name
      t.string :email
    end
  end
end

LIB /任务/ databases.rake

namespace :support do
  desc "Configure the variables that rails need in order to look up for the db configuration in a different folder"
  task :set_custom_db_config_paths do
    ENV['SCHEMA'] = 'db_support/schema.rb'
    Rails.application.config.paths['db'] = ['db_support']
    Rails.application.config.paths['db/migrate'] = ['db_support/migrate']
    Rails.application.config.paths['db/seeds.rb'] = ['db_support/seeds.rb']
    Rails.application.config.paths['config/database'] = ['config/database_support.yml']
  end

  namespace :db do
    task :create => :set_custom_db_config_paths do
        Rake::Task["db:create"].invoke
    end
    task :migrate => :set_custom_db_config_paths do
        Rake::Task["db:migrate"].invoke
    end
    task :rollback => :set_custom_db_config_paths do
        Rake::Task["db:rollback"].invoke
    end
  end

  namespace :test do
    task :check => [:environment] do
        puts "users count = #{User.count}"
        puts "report_users = #{ReportUser.count}"
    end
  end
end

配置完所有这些并运行最后一次rake任务 rake support:test:check, 我收到此错误

ActiveRecord::ConnectionTimeoutError: could not obtain a connection from the pool within 5.000 seconds (waited 5.001 seconds); all pooled connections were in use
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:202:in `block in wait_poll'
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:193:in `loop'
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:193:in `wait_poll'
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:154:in `internal_poll'
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:278:in `internal_poll'
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:148:in `block in poll'
from /Users/harsh/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:158:in `synchronize'
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:148:in `poll'
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:709:in `acquire_connection'
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:501:in `checkout'
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:364:in `connection'
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:875:in `retrieve_connection'
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/connection_handling.rb:128:in `retrieve_connection'
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/connection_handling.rb:91:in `connection'
from /Users/harsh/.rvm/gems/ruby-2.3.1@rails5.0/gems/activerecord-5.0.0.1/lib/active_record/model_schema.rb:354:in `load_schema!'

puts "users count = #{User.count}"工作正常, 但是puts "report_users = #{ReportUser.count}"给出连接超时错误。

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案。 Just Change config/database_support.yml

default: &default
adapter: postgresql
encoding: unicode
pool: 5

development:
  <<: *default
  database: sm_development

test:
  <<: *default
  database: sm_test