将现有的MySQL Rails应用程序转换为使用Postgres:“所有池连接都在使用中”

时间:2019-01-27 17:21:06

标签: mysql ruby-on-rails ruby postgresql

我正在将一个项目从MySQL转换为PostgreSQL。配置它后,当我运行rake db:createrake db:migrate时,出现错误:ActiveRecord::ConnectionTimeoutError: could not obtain a connection from the pool within 5.000 seconds (waited 5.000 seconds); all pooled connections were in use

我尝试将池增加到50。同一台机器上的另一个rails项目使用PostgreSQL也很好。任何想法可能会发生什么?

完整的堆栈跟踪为:

ActiveRecord::ConnectionTimeoutError: could not obtain a connection from the pool within 5.000 seconds (waited 5.000 seconds); all pooled connections were in use
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:202:in `block in wait_poll'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:193:in `loop'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:193:in `wait_poll'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:154:in `internal_poll'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:278:in `internal_poll'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:148:in `block in poll'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:158:in `synchronize'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:148:in `poll'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:747:in `acquire_connection'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:500:in `checkout'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `connection'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `retrieve_connection'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/connection_handling.rb:116:in `retrieve_connection'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/connection_handling.rb:88:in `connection'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/schema_migration.rb:20:in `table_exists?'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/schema_migration.rb:24:in `create_table'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/migration.rb:1125:in `initialize'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/migration.rb:1007:in `new'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/migration.rb:1007:in `up'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/migration.rb:985:in `migrate'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/tasks/database_tasks.rb:171:in `migrate'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/activerecord-5.1.6.1/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/gems/rake-12.3.2/exe/rake:27:in `<top (required)>'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/bin/ruby_executable_hooks:24:in `eval'
/Users/tonyporteous/.rvm/gems/ruby-2.5.3@citydash/bin/ruby_executable_hooks:24:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

配置文件部分如下所示:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: projectx_development

我添加了这样的宝石:

gem 'pg', '>= 0.18', '< 2.0'

1 个答案:

答案 0 :(得分:2)

问题已解决:是由于database.yml文件中的错误引起的:

pool: <%%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

应该是

pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

(我猜是)导致从中提取池大小为0。