Skip to content

Instantly share code, notes, and snippets.

@winebarrel
Created November 9, 2013 06:55
Show Gist options
  • Save winebarrel/7382548 to your computer and use it in GitHub Desktop.
Save winebarrel/7382548 to your computer and use it in GitHub Desktop.
MHAのフェイルオーバー時に再接続するための修正
--- /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.15/lib/active_record/connection_adapters/abstract_mysql_adapter.rb.orig 2013-10-31 07:37:55.704402703 +0000
+++ /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.15/lib/active_record/connection_adapters/abstract_mysql_adapter.rb.reconnect 2013-10-31 09:18:52.061099228 +0000
@@ -239,11 +239,16 @@
# Executes the SQL statement in the context of this connection.
def execute(sql, name = nil)
+ begin
if name == :skip_logging
@connection.query(sql)
else
log(sql, name) { @connection.query(sql) }
end
+ rescue Mysql2::Error
+ sleep 0.3
+ retry
+ end
rescue ActiveRecord::StatementInvalid => exception
if exception.message.split(":").first =~ /Packets out of order/
raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem install mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information. If you're on Windows, use the Instant Rails installer to get the updated mysql bindings."
--- /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.15/lib/active_record/connection_adapters/mysql2_adapter.rb.orig 2013-10-31 09:27:51.300829040 +0000
+++ /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.15/lib/active_record/connection_adapters/mysql2_adapter.rb.reconnect 2013-10-31 09:18:35.885407283 +0000
@@ -13,7 +13,14 @@
config[:flags] = Mysql2::Client::FOUND_ROWS
end
+ client = nil
+
+ begin
client = Mysql2::Client.new(config.symbolize_keys)
+ rescue Mysql2::Error
+ sleep 0.3
+ retry
+ end
options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
ConnectionAdapters::Mysql2Adapter.new(client, logger, options, config)
@@ -250,7 +257,13 @@
private
def connect
+ begin
@connection = Mysql2::Client.new(@config)
+ rescue Mysql2::Error
+ sleep 0.3
+ retry
+ end
+
configure_connection
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment