2014-02-25 40 views
0

嗨,我使用CANCAN Gem用戶角色和的ActiveRecord :: StatementInvalid(RuntimeError:連接不能在派生進程

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

當我一段時間(2〜3分鐘)後刷新broser重複使用它。讓我們ActiveRecord::StatementInvalid (RuntimeError: The connection cannot be reused in the forked process.

任何人都幫我

+0

這是一個網絡服務器下運行? (獨角獸,webrick,乘客,美洲獅......)根據您的網絡服務器,您可能需要在分叉後重新連接。 –

+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文件,它就像一個魅力

0

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

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

並且您可以使用命令unicorn -c config/unicorn.rb運行服務器以使用該配置。

+0

我在config文件夾中添加了一個新文件unicorn.rb,並在運行服務器後粘貼你的代碼,就像這樣使用獨角獸-c config/unicorn.rb它給了我們'':未定義的方法'after_fork'for main:Object(NoMethodError) –

+0

你的Gemfile中有獨角獸嗎? –

+0

是我的gemgile中的獨角獸欄杆 –

相關問題