Skip to content

Instantly share code, notes, and snippets.

@rmrfself
Created February 20, 2012 02:25
Show Gist options
  • Save rmrfself/1867326 to your computer and use it in GitHub Desktop.
Save rmrfself/1867326 to your computer and use it in GitHub Desktop.
Rails mysql reconnect code
ActiveRecord::ConnectionAdapters::MysqlAdapter.module_eval do
def execute_with_retry_once(sql, name = nil)
retried = false
begin
execute_without_retry_once(sql, name)
rescue ActiveRecord::StatementInvalid => exception
ActiveRecord::Base.logger.info "#{exception}, retried? #{retried}"
# Our database connection has gone away, reconnect and retry this method
reconnect!
unless retried
retried = true
retry
end
end
end
alias_method_chain :execute, :retry_once
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment