Skip to content

Instantly share code, notes, and snippets.

@ihassin
Created June 3, 2012 14:51
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihassin/2863808 to your computer and use it in GitHub Desktop.
Save ihassin/2863808 to your computer and use it in GitHub Desktop.
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