/ Published in: Ruby
Expand |
Embed | Plain Text
# Patch Mysql adapter so that it reconnects after discovering an lost connection error module ActiveRecord::ConnectionAdapters class MysqlAdapter alias :orig_execute :execute def execute(sql,name=nil) orig_execute(sql,name) rescue ActiveRecord::StatementInvalid => exception if LOST_CONNECTION_ERROR_MESSAGES.any? { |msg| exception.message. =~ /#{msg}/ } reconnect! retry else raise end end end end
Comments
Subscribe to comments
You need to login to post a comment.

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.