Posted By

esad on 11/26/07


Tagged

mysql rails activerecord monkeypatch


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

darthomir
webstic


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


 / Published in: Ruby
 

  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
Posted By: fjfish on December 8, 2008

Just one thing:

You might want to try maybe 3 times and then pass the exception up anyway, if the database really has gone away this will sit there looping forever. Usually I also wait a second or so before retrying something like this, but this might not be that good an idea if you're trying to respond to an http request.

Posted By: on February 21, 2019

android root bbs

You need to login to post a comment.