I'm experiencing a few errors on a rails app, along the lines of:
ActiveRecord::StatementInvalid: Mysql::Error: Lost connection to MySQL server during query: SELECT * FROM `actions` WHERE (`foo`.`id` = 16)
What appears to be happening is that mysql connection is being closed after a timeout, and rails isn't noticing until it's too late.
The remedies I find appear to be to set the reconnect flag to true in database.yaml, or for any database action adding some code like so:
def some_database_operation
  begin
    Account.find(1)
    # or some other database operations here...
  rescue ActiveRecord::StatementInvalid
    ActiveRecord::Base.connection.reconnect!
    unless @already_retried
      @already_retried = true
      retry 
    end
    raise
  else
    @already_retried = false
  end
end
end
I'm listing this option over this one visible here, because this option is apparently unsafe for transactions:
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
Of the options to avoid this annoying error, the reconnect option in the yaml file seems by the far the tidiest option - but I'm curious; why you wouldn't set this value to be true by default in your database?
I'd rather not solve one problem by causing a load of others further down the line.
Thanks,