title: Rails 3.1.0 and Resque -- "Mysql2::Error: MySQL server has gone away"
body: <p>Help! I keep getting "MySQL server has gone away" errors at what seems like random times. The same worker will run sometimes and then crash this way the next. I can't seem to find a pattern to the madness. </p>
  
  <p>I have tried the following solutions:</p>
  
  <pre><code>ActiveRecord::Base.verify_active_connections!
  </code></pre>
  
  <p>in my perform methods in each task, as per the <a href="https://github.com/defunkt/resque/wiki/FAQ" rel="nofollow noreferrer">resque faq</a>.</p>
  
  <p>I have also tried this code: </p>
  
  <pre><code>adapter = ActiveRecord::Base.configurations[Rails.env]['adapter']
  
  if adapter == "mysql2"
   module ActiveRecord::ConnectionAdapters
     class Mysql2Adapter
       alias_method :execute_without_retry, :execute
  
       def execute(*args)
         execute_without_retry(*args)
       rescue ActiveRecord::StatementInvalid =&gt; e
         if e.message =~ /server has gone away/i
           warn "Server timed out, retrying"
           reconnect!
           retry
         else
           raise e
         end
       end
     end
   end
  end
  </code></pre>
  
  <p>in an initializer, as <a href="https://gist.github.com/238999" rel="nofollow noreferrer">per code by a Resque developer</a>, with some modifications found on StackOverflow and elsewhere. </p>
  
  <p>I have also tried both of the above together. The results are the same. </p>
  
  <p>I also tried setting <code>wait_timeout: 2147483</code> in my database.yml as per <a href="https://stackoverflow.com/questions/8610426/rails-3-1-mysql2-error-mysql-server-has-gone-away">this StackOverflow post</a> to no avail. </p>
  
  <p>I am using:</p>
  
  <ul>
  <li>rails 3.1.0 </li>
  <li>resque 1.20.0 </li>
  <li>resque-scheduler 1.9.9 </li>
  <li>mysql2 0.3.11</li>
  </ul>
  
  <p>Everything is running on a Mac with RVM/Homebrew/Passenger. The mysql database is local to my machine.</p>
  
  <p>Here is an example of an error:</p>
  
  <pre><code>Mysql2::Error: MySQL server has gone away: SHOW FIELDS FROM `m_variables`
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `block in execute'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `execute'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:473:in `columns'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `block (2 levels) in initialize'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:185:in `with_connection'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:92:in `block in initialize'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:106:in `yield'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:106:in `default'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:106:in `block in initialize'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/base.rb:711:in `yield'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/base.rb:711:in `default'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/base.rb:711:in `columns_hash'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/locking/optimistic.rb:145:in `locking_enabled?'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/relation.rb:110:in `to_a'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/relation/finder_methods.rb:155:in `all'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/base.rb:441:in `all'
  /Users/fdot/Sites/mq/app/workers/m_variable_type_assigner.rb:25:in `perform'
  </code></pre>
  
  <p>and here is another example:</p>
  
  <pre><code>Mysql2::Error: MySQL server has gone away: SELECT `users`.* FROM `users`
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:687:in `query'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:687:in `block in exec_query'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:685:in `exec_query'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:679:in `select'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/base.rb:470:in `find_by_sql'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/relation.rb:111:in `to_a'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/relation/finder_methods.rb:155:in `all'
  /Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/base.rb:441:in `all'
  /Users/fdot/Sites/mq/app/workers/transient_user_cleaner.rb:21:in `perform'
  </code></pre>
  
  <p>I'm at a loss here. Help is much appreciated!</p>