ActiveRecord连接死于随机Ruby方法(.size,.each)

时间:2013-10-07 16:21:14

标签: ruby-on-rails activerecord

我使用ActiveRecord版本3.2.14(并尝试使用3.2.15 rc)和TinyTDS + activerecord sql adapter连接到我的SQL Server数据库。

我遇到了引发此异常的非常奇怪的行为:

/usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record
/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection':
ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)

但根据追踪:

from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/model_schema.rb:229:in `columns'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/model_schema.rb:249:in `column_names'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/model_schema.rb:262:in `column_methods_hash'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/dynamic_matchers.rb:74:in `all_attributes_exists?'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/dynamic_matchers.rb:27:in `method_missing'
from /usr/local/debugging-exchange-test/app/controllers/new_orders_controller.rb:15:in `block in start'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/relation/delegation.rb:6:in `each'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.14/lib/active_record/relation/delegation.rb:6:in `each'
from /usr/local/debugging-exchange-test/app/controllers/new_orders_controller.rb:13:in `start'
from /usr/local/debugging-exchange-test/lib/exchange.rb:57:in `testing'
from /usr/local/debugging-exchange-test/lib/exchange.rb:39:in `initialize'
from /usr/local/debugging-exchange-test/lib/exchange.rb:104:in `new'
from /usr/local/debugging-exchange-test/lib/exchange.rb:104:in `<top (required)>'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application.rb:203:in `load'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application.rb:203:in `start_load'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/application.rb:298:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/controller.rb:70:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons.rb:147:in `block in run'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/cmdline.rb:109:in `call'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons/cmdline.rb:109:in `catch_exceptions'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/daemons-1.1.9/lib/daemons.rb:146:in `run'
from ./data_exchange:15:in `<main>'

它发生在each方法:

def start
  web_queue = Nti::Order.where("orderid IS NOT NULL AND updated_by_exchange_at IS NULL AND orderstatus <> 9")
  logger.info "web_queue size: #{web_queue.length}"
  web_queue.each do |nti_order| #<---- this line
    # logger.info nti_order.orderid
    web_order = Web::Order.find_by_id(nti_order.orderid)
    raise "Couldn't find web order with ID #{nti_order.orderid}" if web_order.nil?
    web_order.status = 99
    web_order.save!
    logger.info "Web order with ID #{web_order.id} updated to status = 99"
  end
end

在此之前,由于.size方法,我得到了相同的错误,但是这条线:

logger.info "web_queue size: #{web_queue.size}"

当我将其更改为.length时,它可以正常工作。到底他妈发生了什么?这些方法与ActiveRecord无关。我的Array方法正在抛出ActiveRecord异常。

BTW这是一个使用activerecord在两个数据库之间交换数据的ruby应用程序(不是rails)。

只是为了表明数据库连接有效,当我改变方法时:

def start
  a = Nti::Order.where("orderid IS NOT NULL AND updated_by_exchange_at IS NULL AND orderstatus <> 9")
  logger.info a.first.inspect
  logger.info "done"
end

它有效。这也有效:

def start
  a = Nti::Order.where("orderid IS NOT NULL AND updated_by_exchange_at IS NULL AND orderstatus <> 9")
  # logger.info a.size
  puts a.map{ |order| order.orderid }

  logger.info "done"
end

任何人都知道发生了什么事?

1 个答案:

答案 0 :(得分:0)

尝试增加database.yml中的poolsize。 将默认pool: 5更改为较大的内容。