A monkey-patch to activerecord to reconnect after it loses the connection to the mysql server


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # Patch Mysql adapter so that it reconnects after discovering an lost connection error
  2.  
  3. module ActiveRecord::ConnectionAdapters
  4. class MysqlAdapter
  5. alias :orig_execute :execute
  6. def execute(sql,name=nil)
  7. orig_execute(sql,name)
  8. rescue ActiveRecord::StatementInvalid => exception
  9. if LOST_CONNECTION_ERROR_MESSAGES.any? { |msg| exception.message. =~ /#{msg}/ }
  10. reconnect!
  11. retry
  12. else
  13. raise
  14. end
  15. end
  16. end
  17. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.