简体   繁体   English

在Ruby On Rails中改变SQL查询结果

[英]Varying SQL Query Result in Ruby On Rails

I have a function: 我有一个功能:

    def self.run_all_scheduled_reports
            scheduled_queries = Reports.find(
                :all,
                :joins=> 'INNER JOIN `expected_times` 
                 ON (`reports`.`report_id` =`expected_times`.`report_id`)')

            if scheduled_queries.empty?
                print "No reports to run at this time ("+Time.now.to_s+")"
            else
                caught_exception = false
                ActiveRecord::Base.connection.execute("SET SESSION WAIT_TIMEOUT=2400");
                scheduled_queries.each do |query|
                    begin
                        print "Running report ("+query[:id].to_s+")...\n"
                        self.run_report(query[:id])
                    rescue Exception => e
                        caught_exception = true
                        print e
                    end
                end
                raise "At least one query resulted in an Exception!" if caught_exception
            end
        end

The weird thing is that, when i run this function consecutively (via script/runner -e ...), the result varies. 奇怪的是,当我连续运行此函数(通过脚本/运行器-e ...)时,结果有所不同。 Sometimes about 30 reports are printed while sometimes just 15 (and sometimes, no results at all are returned). 有时大约打印30个报告,而有时只打印15个报告(有时根本不返回结果)。 You guys have any idea whats happening? 你们知道发生了什么吗? My DB is MYSQL. 我的数据库是MYSQL。

Also, if i change the environment to "development" OR if i omit the JOIN part, its working pretty well. 另外,如果我将环境更改为“开发”,或者如果我省略了JOIN部分,那么它的效果也很好。

I just solved the issue, the inconsistency happens because apparently, my rails is set to use a separate layer/framework to do the db transactions. 我只是解决了这个问题,所以发生了不一致,因为显然,我的rails设置为使用单独的图层/框架来执行数据库事务。 This middle layer randomly reads from 2 seperate MYSQL boxes, which turned out to be out of sync. 该中间层从2个单独的MYSQL框中随机读取,结果发现它们不同步。 Once i synced the 2 boxes, the problem was fixed. 一旦我同步了两个盒子,问题就解决了。 Thanks for all the help 谢谢你的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM