某處在我們的Ruby代碼時,有三條線是(目前,用於調試)是這樣的:「的Mysql ::錯誤:查詢:未連接」使用兩個不同的數據庫
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
當這代碼被擊中,第一行始終打印
Established connection to: {:username=>"name", :host=>"localhost", :password=>"pass",
:adapter=>"mysql", :database=>"database1"}
通知我們連接已成功建立。通常,第二行和第三行按預期執行,一切順利。然後,第二行產生相同樣的結果:
Connection is connected to: {:username=>"name", :host=>"localhost", :password=>"pass",
:adapter=>"mysql", :database=>"database1"}
然而,出於某種原因,一旦在每一段時間(例如,幾百個測試用例的執行過程中兩次),第二生產線生產:
Connection is connected to: {:username=>"name", :host=>"localhost", :password=>"pass",
:adapter=>"mysql", :database=>"database2"}
(注意不同的數據庫)和第三線失敗,並
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)
(我想你可以忽略「StatementInvalid」中就有:這是一個有效的中號ySQL查詢,並在該消息中使用StatementInvalid似乎在ActiveRecord的代碼中的小bug)
所以,出於某種原因,建立連接和獲取綁定到線程的連接之間,ActiveRecord的迷糊。有誰知道爲什麼會發生這種情況?
CentOS Linux,Ruby 1.8.7(企業版2011.03),Rails 3.0.4,MySQL 2.8.1。
幾乎所有在網絡上發現的錯誤都涉及在Windows系統上降級mysql DLL,這在此不適用。