2011-03-11 103 views
2

我使用Parallel寶石與Rails3中,並與MySQL線程獲取的問題,即使是一個簡單的一行:的ActiveRecord :: StatementInvalid的:mysql ::錯誤:MySQL服務器已消失:(使用並行寶石)

Parallel.each(User.all, :in_processes => 1) { |r| puts r.username } 

它交替工作,然後通過第二次失敗。以下是錯誤我得到:

ruby-1.8.7-p330 :035 > Parallel.each(User.all, :in_processes => 1) { |r| puts r.username } 
ActiveRecord::StatementInvalid: Mysql::Error: MySQL server has gone away: SELECT `users`.* FROM `users` 
    from /Users/kimptoc/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `log' 
    from /Users/kimptoc/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:289:in `execute' 
    from /Users/kimptoc/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:619:in `select' 
    from /Users/kimptoc/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all' 
    from /Users/kimptoc/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/query_cache.rb:56:in `select_all' 
    from /Users/kimptoc/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/base.rb:467:in `find_by_sql' 
    from /Users/kimptoc/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/relation.rb:64:in `to_a' 
    from /Users/kimptoc/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/relation/finder_methods.rb:143:in `all' 
    from /Users/kimptoc/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/base.rb:439:in `__send__' 
    from /Users/kimptoc/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/base.rb:439:in `all' 
    from (irb):35 
    from (null):0 

如果我做不平行的版本,它工作得很好:

User.all.each { |r| puts r.username } 

我使用的是MySQL的寶石,但試圖mysql2和的MySQLPlus。

在OSX上運行。

我在想如何ActiveRecord和MySQL gem與線程一起工作的問題。

從我讀過的內容來看,這可能是我需要調整mysql設置,以使其更具併發性。儘管替代寶石似乎更好地解決了處理併發問題。

我提出這與寶石查詢 - https://github.com/grosser/parallel/issues/9#comment_844380 - 但這似乎更像是如何設置MySQL的紅寶石的併發訪問一個根本性的問題...

所以,我的問題是 - 有一個Rails3和MySQL的併發數據庫訪問權限配置?

謝謝,克里斯

編輯

什麼似乎是工作被分成2個查詢,一到得到的ID,然後通過ID的並聯環路內循環,再訪問通過id實體。

ids = User.all.map { |u| u.id } 
Parallel.each(ids, :in_processes => 1) do |uid| 
    ActiveRecord::Base.connection.reconnect! 
    r = User.find(uid) 
    puts r.username 
end 

回答

3

您需要在分叉後建立連接。這是分叉的「特徵」 - 網絡連接處於不一致的狀態。

Parallel.each(User.all, :in_processes => 1) do |r| 
    ::ActiveRecord::Base.establish_connection 
    puts r.username 
end 
+0

謝謝 - 有道理,會嘗試一下。 – 2011-03-11 15:18:17

+0

嗯 - 仍然出現錯誤,我想它是由於集合直接鏈接到數據庫。也許我應該拔出ID然後重新訪問循環內的對象... – 2011-03-11 17:51:40

2

我得到一個非常類似的錯誤有以下幾點:

pid = Process.fork 
if pid 
    Process.detach(pid) 
else 
    # Perform long task using ActiveRecord 
    do_stuff 
end 

如果我打了一個請求的服務器,而do_stuff正在運行,它會殺死任務,並拋出一個異常:

ActiveRecord::StatementInvalid (Mysql2::Error: Lost connection to MySQL server during query: ... 

加入弗朗索瓦的建議,固定我的問題:

pid = Process.fork 
if pid 
    Process.detach(pid) 
else 
    # Perform long task using ActiveRecord 
    ActiveRecord::Base.establish_connection 
    do_stuff 
end 

謝謝弗朗索瓦!

相關問題