2016-04-05 11 views
1

私のテストでは、いくつかのSQLクエリを別のスレッドで実行する際に問題があります。私は、単純なselectとcountをSQLで実行すると同じ結果を返すと予想しますが、これらのメソッドをスレッド(process_new_followersprocess_lost_followers)で実行すると、異なる結果が返されます。RubyマルチスレッドSQL(テスト環境)

def compute_followers 
    followers = Follower.where({ user_id: user_id, is_following: true }).count # => returns 10 always 

    calculate_differences 
    thread1 = Thread.new { process_new_followers } 
    thread2 = Thread.new { process_lost_followers } 
    thread1.join 
    thread2.join 

end 

def process_new_followers 
    puts Follower.where({ user_id: user_id, is_following: true }).count # => returns 0 if threaded and 10 if not 
    ... I commented out this code so it only prints the count and nothing changes, so no db changes are happening in this method 
end 

def process_lost_followers 
    puts Follower.where({ user_id: user_id, is_following: true }).count # => returns 0 if threaded and 10 if not 
    ... I commented out this code so it only prints the count and nothing changes, so no db changes are happening in this method 
end 

なぜスレッディング効果は、このSQLクエリになります。ここでは

は(基本的な)コードのですか?

答えて

2

これはActiveRecordを使用するRailsアプリケーションにあるため、Railsはそのテストスイート内のすべてのトランザクションをロールバックします。あなたはスレッドの中でそれを踏んでいるので、トランザクションはおそらく既にロールバックされている可能性があります。なぜスレッド数が0で、スレッドなしの呼び出し数が10であるのかを説明します。

関連する問題