“MySQL server has gone away” with Ruby on Rails

后端 未结 11 1304
后悔当初
后悔当初 2020-12-12 19:09

After our Ruby on Rails application has run for a while, it starts throwing 500s with \"MySQL server has gone away\". Often this happens overnight. It\'s started doing this

11条回答
  •  不思量自难忘°
    2020-12-12 20:09

    This is probably caused by the persistent connections to MySQL going away (time out is likely if it's happening over night) and Ruby on Rails is failing to restore the connection, which it should be doing by default:

    In the file vendor/rails/actionpack/lib/action_controller/dispatcher.rb is the code:

    if defined?(ActiveRecord)
      before_dispatch { ActiveRecord::Base.verify_active_connections! }
      to_prepare(:activerecord_instantiate_observers) {ActiveRecord::Base.instantiate_observers }
    end
    

    The method verify_active_connections! performs several actions, one of which is to recreate any expired connections.

    The most likely cause of this error is that this is because a monkey patch has redefined the dispatcher to not call verify_active_connections!, or verify_active_connections! has been changed, etc.

提交回复
热议问题