gpt4 book ai didi

ruby-on-rails - ActiveRecord::ConnectionTimeoutError:无法在 5.000 秒内从池中获取连接。所有池连接都在使用中

转载 作者:太空宇宙 更新时间:2023-11-03 16:42:13 25 4
gpt4 key购买 nike

我正在尝试在单个 Rails 应用程序中使用多个数据库。我创建了一个模型 ReportUser 来访问在不同数据库中定义的 report_users 表,该表已在 config/support_database.yml 中指定。正常模型 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

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/migrate/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 任务之后rake 支持:测试:检查,我收到这个错误

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

最佳答案

我终于找到了解决办法。只需更改 config/database_support.yml

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

development:
<<: *default
database: sm_development

test:
<<: *default
database: sm_test

关于ruby-on-rails - ActiveRecord::ConnectionTimeoutError:无法在 5.000 秒内从池中获取连接。所有池连接都在使用中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44099036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com