Skip to content

Instantly share code, notes, and snippets.

@ihassin

ihassin/Reconnect

Created Jun 3, 2012
Embed
What would you like to do?
Reconnect to a lost database connection using an exception block trick (ruby, rails, mysql, activerecord)
def my_task
while(true) do
begin
database_access_here
rescue Exception => ex
begin
ActiveRecord::Base.connection.reconnect!
rescue
sleep 10
retry # will retry the reconnect
else
retry # will retry the database_access_here call
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment