From 8545eb1abeb0cded294d0546caefa119d04cbe15 Mon Sep 17 00:00:00 2001 From: Tim Morgan Date: Wed, 16 Aug 2023 13:33:17 -0500 Subject: [PATCH] Also handle ActiveRecord::DatabaseConnectionError --- lib/activerecord/mysql/reconnect.rb | 2 ++ spec/activerecord-mysql-reconnect_spec.rb | 41 ++++++++--------------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/lib/activerecord/mysql/reconnect.rb b/lib/activerecord/mysql/reconnect.rb index 2a3322f..4717f5f 100644 --- a/lib/activerecord/mysql/reconnect.rb +++ b/lib/activerecord/mysql/reconnect.rb @@ -26,6 +26,7 @@ module Activerecord::Mysql::Reconnect HANDLE_ERROR = [ ActiveRecord::ConnectionNotEstablished, + ActiveRecord::DatabaseConnectionError, ActiveRecord::StatementInvalid, Mysql2::Error, ] @@ -47,6 +48,7 @@ module Activerecord::Mysql::Reconnect lost_connection: "Lost connection to MySQL server at 'reading initial communication packet'", not_connected: "MySQL client is not connected", killed: 'Connection was killed', + issue_connecting: 'There is an issue connecting with your hostname', } READ_SQL_REGEXP = /\A\s*(?:SELECT|SHOW|SET)\b/i diff --git a/spec/activerecord-mysql-reconnect_spec.rb b/spec/activerecord-mysql-reconnect_spec.rb index cba2ac4..8d09292 100644 --- a/spec/activerecord-mysql-reconnect_spec.rb +++ b/spec/activerecord-mysql-reconnect_spec.rb @@ -452,38 +452,32 @@ end let(:mysql_error) do - Mysql2::Error.const_defined?(:ConnectionError) ? Mysql2::Error::ConnectionError : Mysql2::Error + Mysql2::Error::ConnectionError end before do allow_any_instance_of(mysql_error).to receive(:message).and_return('Lost connection to MySQL server during query') + allow_any_instance_of(ActiveRecord::StatementInvalid).to receive(:message).and_return('Lost connection to MySQL server during query') + allow_any_instance_of(ActiveRecord::DatabaseConnectionError).to receive(:message).and_return('Lost connection to MySQL server during query') end context "when retry failed " do specify do - if ActiveRecord::VERSION::MAJOR < 6 - expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [ - "MySQL server has gone away. Trying to reconnect in 0.5 seconds.", - "#{mysql_error}: Lost connection to MySQL server during query: SELECT `employees`.* FROM `employees` [ActiveRecord::StatementInvalid]", - ]) - else - expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [ - "MySQL server has gone away. Trying to reconnect in 0.5 seconds.", - "#{mysql_error}: Lost connection to MySQL server during query [ActiveRecord::StatementInvalid]", - ]) - end - + expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [ + "MySQL server has gone away. Trying to reconnect in 0.5 seconds.", + "Lost connection to MySQL server during query [ActiveRecord::StatementInvalid]", + ]) (1.0..4.5).step(0.5).each do |sec| expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [ "MySQL server has gone away. Trying to reconnect in #{sec} seconds.", - "Lost connection to MySQL server during query [#{mysql_error}]", + "Lost connection to MySQL server during query [ActiveRecord::DatabaseConnectionError]", ]) end expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [ "Query retry failed.", - "Lost connection to MySQL server during query [#{mysql_error}]", + "Lost connection to MySQL server during query [ActiveRecord::DatabaseConnectionError]", ]) expect(Employee.all.length).to eq 1000 @@ -491,23 +485,16 @@ expect { Employee.all.length - }.to raise_error(mysql_error) + }.to raise_error(ActiveRecord::DatabaseConnectionError) end end context "when retry succeeded" do specify do - if ActiveRecord::VERSION::MAJOR < 6 - expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [ - "MySQL server has gone away. Trying to reconnect in 0.5 seconds.", - "#{mysql_error}: Lost connection to MySQL server during query: SELECT `employees`.* FROM `employees` [ActiveRecord::StatementInvalid]", - ]) - else - expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [ - "MySQL server has gone away. Trying to reconnect in 0.5 seconds.", - "#{mysql_error}: Lost connection to MySQL server during query [ActiveRecord::StatementInvalid]", - ]) - end + expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [ + "MySQL server has gone away. Trying to reconnect in 0.5 seconds.", + "Lost connection to MySQL server during query [ActiveRecord::StatementInvalid]", + ]) expect(Employee.all.length).to eq 1000 MysqlServer.restart