【问题标题】:RuntimeError: The connection cannot be reused in the forked process (ruby-oci8 gem)RuntimeError:无法在分叉进程中重用连接(ruby-oci8 gem)
【发布时间】:2018-01-15 07:44:55
【问题描述】:

我得到了

RuntimeError: The connection cannot be reused in the forked process

来自

ruby-oci8 (2.1.3) lib/oci8/cursor.rb:28:in `__initialize'

我正在将 oracle db 用于 rails 应用程序,最近我开始使用 unicornnginx 部署 rails 应用程序,从他们开始我收到此错误,请帮助。

我正在使用ruby 1.9.3rails 3.1,这是我的unicorn.rb 文件

rails_env = ENV['RAILS_ENV'] || 'production'

worker_processes 6

preload_app true

timeout 75

app_dir     = File.expand_path("../../..", __FILE__)
shared_path = "#{app_dir}/shared"
working_directory "#{app_dir}/current"

# Set up socket location
listen "#{shared_path}/sockets/unicorn.sock", :backlog => 64

# Set master PID location
pid "#{shared_path}/pids/unicorn.pid"

stderr_path "#{shared_path}/log/unicorn.log"
stdout_path "#{shared_path}/log/unicorn.log"

before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  # reset the connection since the pre-forked connection will be stale
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  if defined?(ActiveRecord::Base)
    config = ActiveRecord::Base.configurations[Rails.env] ||
            Rails.application.config.database_configuration[Rails.env]
    config['pool'] = ENV['DB_POOL'] || 5
    ActiveRecord::Base.establish_connection(config)
  end
end

【问题讨论】:

  • 这似乎与您的 gem 无关,而与您的服务器堆栈有关。你可能会从这篇文章中得到一些有用的信息Deploying Rails Applications with Unicorn
  • unicorn.rb 中添加了相同的配置,但仍然出现相同的错误。
  • 对于一个架构我没有收到任何错误,对于一个具有相同用户名和密码的特定架构,我收到了错误。

标签: ruby-on-rails ruby nginx unicorn


【解决方案1】:

我得到了解决方案。基本上我的模型包含像这样的establish_connection 定义

class User < ActiveRecord::Base
  establish_connection "user_#{Rails.env}"
end

我的unicorn.rb 文件before_fork 块包含以下代码。

defined?(ActiveRecord::Base) and
  ActiveRecord::Base.connection.disconnect!

我发现当您在模型中明确写入 establish_connection 时,上述代码不起作用。为了解决这个问题,我不得不将上面的代码更改为下面的代码

defined?(ActiveRecord::Base) &&
  ActiveRecord::Base.remove_connection(User) &&
  ActiveRecord::Base.connection.disconnect!

它就像一个魅力。 :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2017-03-20
    • 2019-06-12
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多