gpt4 book ai didi

mysql - 使用连接池处理事件记录超时的正确方法是什么?

转载 作者:IT老高 更新时间:2023-10-29 00:08:34 29 4
gpt4 key购买 nike

我已经找到了一个奇怪的错误 undefined method `run_callbacks' for nil:NilClass 并且能够用这个示例代码重现它。

基本上问题是事件记录超时(默认为 5 秒)但抛出未定义的方法异常,这对我来说似乎是错误的。

但是无论如何,处理这个问题的正确方法是什么?在我的真实代码中,我有一堆忙于做实际工作的线程,但偶尔我会遇到这个错误。所以想象一下 puts 是真正的代码。我希望现有线程在发生这种情况时继续工作。

threads = []
10.times do |n|

threads << Thread.new {
ActiveRecord::Base.connection_pool.with_connection do |conn|
puts "#{n} #{conn}"
res = conn.execute("select sleep(6)", :async => true)
end
}
end

# block and wait for all threads to finish
threads.each { |t| puts "joined" ; t.join }
rescue Exception => e
puts $!, $@
end

如果我按原样运行这段代码,我会得到异常。如果我将 sleep 时间减少到 4 秒,我就不会。这是 6s sleep 的输出。

joined
0 #<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0xb73c6380>
1 #<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0xb73c5548>
2 #<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0xb73c4fe4>
3 #<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0xb73c4a80>
4 #<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0xb73c451c>
joined
joined
joined
joined
joined
undefined method `run_callbacks' for nil:NilClass
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:212:in `checkin'
sqltst.rb:31:in `join'
sqltst.rb:31
sqltst.rb:31:in `each'
sqltst.rb:31

最佳答案

自从发布此问题以来,ActiveRecord 在这方面做了很多工作。这里有一些很好的解释:http://bibwild.wordpress.com/2012/03/15/activerecord-concurrency-currently-good-news-and-bad/

关于mysql - 使用连接池处理事件记录超时的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4915320/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com