Somewhere in our Ruby code, there are three lines that (currently, for debugging purposes) look like this:
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)
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?
CentOS Linux, Ruby 1.8.7 (Enterprise Edition 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.
The cause was an issue in the cucumber-rails gem, as filed here: https://github.com/cucumber/cucumber-rails/issues/152