ActiveRecord :: StatementInvalid(RuntimeError:无法在分叉进程中重用连接

时间:2014-02-25 11:32:03

标签: ruby-on-rails ruby oracle cancan

您好我使用CANCAN Gem作为用户角色和

database ->oracle
oracle adaptor - > oracle_enhanced_adaptor 1.4.1
ruby 1.9.3
rails 3.2.16
web server -> unicorn

当我在一段时间(2或3分钟)后刷新更亮。它给了我们ActiveRecord::StatementInvalid (RuntimeError: The connection cannot be reused in the forked process.

任何人帮助我

2 个答案:

答案 0 :(得分:1)

我从谷歌社区找到了答案

dbconfig = ActiveRecord::Base.remove_connection
child_pid = fork do
# establish new db connection for forked child
  ActiveRecord::Base.establish_connection(dbconfig)
   # do stuff...
end
# re-establish db connection
ActiveRecord::Base.establish_connection(dbconfig)
# detach the process so we don't wait for it
Process.detach(child_pid)

environment.rb文件中,它就像魅力一样

答案 1 :(得分:0)

您需要将此添加到您的unicorn.rb文件

#config/unicorn.rb
after_fork do |server, worker|
  defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection
end

您可以使用命令unicorn -c config/unicorn.rb运行服务器以使用配置。