活动记录::连接超时错误:无法在 5.000 秒内从池中获取连接. 所有池连接都在使用中



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

以下是我遵循的几件事。

创建了一个不同的配置/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

config/initializers/general.rb

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

app/models/support_base.rb

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

app/models/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/tasks/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 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}"给出连接超时错误。

终于找到了解决方案。Just Change config/database_support.yml

default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: sm_development
test:
<<: *default
database: sm_test

相关内容

  • 没有找到相关文章

最新更新