Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL client is not connected #39

Closed
kuldeepaggarwal opened this issue May 21, 2021 · 2 comments
Closed

MySQL client is not connected #39

kuldeepaggarwal opened this issue May 21, 2021 · 2 comments

Comments

@kuldeepaggarwal
Copy link

Hi team, during the testing I found a very unusual behaviour of Toxiproxy#down method. The following snippet causes the MySQL client is not connected error:

test "#record_create throws a SQL exception if mysql_master database is down" do
  record = Record.new(attributes)
  Toxiproxy[:mysql_master].down do
    assert_raises(ActiveRecord::ConnectionNotEstablished, ActiveRecord::StatementInvalid) do
       create_record(record)
    end
  end
  Record.first # => raising an error, i.e. "ActiveRecord::ConnectionNotEstablished: MySQL client is not connected"
end
@miry
Copy link
Contributor

miry commented Mar 16, 2022

@kuldeepaggarwal can you provide your expectations?

@DougEdey
Copy link

@kuldeepaggarwal the issue is that ActiveRecord caches the connection, after you've taken down the DB and run a query it breaks the connection that's cached, you need to do an ActiveRecord::Base.connection.reset! (or there's probably a better equivalent) to reset the cached connection.

I've got a ToxiproxyHelper module in my apps that sets this up as a teardown:

 def self.included(base)
    return unless base.ancestors.include?(ActiveSupport::TestCase)

    base.class_eval do
      teardown do
        Toxiproxy.reset
        # Rails caches the connection which we may have broken
        ActiveRecord::Base.connection.reset!
      end
    end
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants