使用两个不同的数据库时,“Mysql :: Error:query:not connected”

时间:2022-09-21 23:49:25

Somewhere in our Ruby code, there are three lines that (currently, for debugging purposes) look like this:

在Ruby代码的某处,有三行(目前用于调试目的)如下所示:

puts "Established connection to: " \
    "#{ActiveRecord::Base.establish_connection(database_config).spec.config}"
puts "Connection is connected to: " \
    "#{ActiveRecord::Base.connection.instance_eval{@config.inspect}}
ActiveRecord::Base.connection.execute("SELECT 1") # Test connection

When this code is hit, the first line always prints

当命中此代码时,第一行始终打印

Established connection to: {:username=>"name", :host=>"localhost", :password=>"pass",
                            :adapter=>"mysql", :database=>"database1"}

informing us that the connection was established successfully. Usually, the second and third lines are executed as expected and everything goes its merry way. The second line then produces the same kind of result:

通知我们已成功建立连接。通常,第二行和第三行按预期执行,一切都顺利进行。然后第二行产生相同类型的结果:

Connection is connected to: {:username=>"name", :host=>"localhost", :password=>"pass",
                            :adapter=>"mysql", :database=>"database1"}

However, for some reason, once in every while (for instance, twice during execution of a few hundred testcases), the second line produces:

但是,由于某种原因,每隔一段时间(例如,在执行几百个测试用例期间两次),第二行产生:

Connection is connected to: {:username=>"name", :host=>"localhost", :password=>"pass",
                            :adapter=>"mysql", :database=>"database2"}

(note the different database) and the third line fails with

(注意不同的数据库),第三行失败了

Mysql::Error: query: not connected: SELECT 1
$APP_ROOT/vendor/bundle/ruby/1.8/gems/activerecord-3.0.4/lib/active_record/connection_adapters/abstract_adapter.rb:207:in `log': Mysql::Error: query: not connected: SELECT 1 (ActiveRecord::StatementInvalid)
from $APP_ROOT/vendor/bundle/ruby/1.8/gems/activerecord-3.0.4/lib/active_record/connection_adapters/mysql_adapter.rb:289:in `execute'
from $APP_ROOT/somewhere_in_our_code.rb:84:in `establish_database_connection' (which is the third line quoted above)

(I think you can disregard the "StatementInvalid" in there: this is a valid MySQL query and the use of StatementInvalid in this message seems a minor bug in the ActiveRecord code)

(我想你可以忽略那里的“StatementInvalid”:这是一个有效的MySQL查询,在这条消息中使用StatementInvalid似乎是ActiveRecord代码中的一个小错误)

So, for some reason, between establishing the connection and getting a connection to bind to the thread, ActiveRecord gets confused. Does anyone know why this could be happenening?

因此,出于某种原因,在建立连接和获取绑定到线程的连接之间,ActiveRecord会感到困惑。有谁知道为什么会发生这种情况?

CentOS Linux, Ruby 1.8.7 (Enterprise Edition 2011.03), Rails 3.0.4, MySQL 2.8.1.

CentOS Linux,Ruby 1.8.7(企业版2011.03),Rails 3.0.4,MySQL 2.8.1。

Almost all cases of this error you can find on the web concern downgrading the mysql DLL on Windows systems, which does not apply here.

您可以在Web上找到的此错误的几乎所有情况都涉及降级Windows系统上的mysql DLL,这在此处不适用。

1 个解决方案

#1


0  

The cause was an issue in the cucumber-rails gem, as filed here: https://github.com/cucumber/cucumber-rails/issues/152

导致这是一个问题,在这里提交的瓜黄瓜宝石:https://github.com/cucumber/cucumber-rails/issues/152

#1


0  

The cause was an issue in the cucumber-rails gem, as filed here: https://github.com/cucumber/cucumber-rails/issues/152

导致这是一个问题,在这里提交的瓜黄瓜宝石:https://github.com/cucumber/cucumber-rails/issues/152