Created
June 3, 2012 14:51
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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