I'm still working on recovering from a Mysql::Error (MySQL server has
gone away: SELECT...). I constructed a function to wrap around Active
Record finds, which would rescue Mysql::Error, log the event, and
attempt to reconnect after a pause.
The rescue does not trap the exception. Can anyone help me figure out
why? The code sample below shows what's on the call stack.
This is a non-web application, using ActiveRecord stand-alone.
The next thing I'm going to try is "rescue Exception => e". However,
this error is rare, so I'm asking for your help in parallel.
$ rails --version
Rails 2.2.2
$ ruby -v
ruby 1.8.5 (2006-08-25) [x86_64-linux]
— F
==
def catching_mysql_error
retval = true
begin
yield # The exception is raised inside this "yield."
rescue Mysql::Error => e
# PROBLEM: This rescue is never reached on a Mysql::Error
# (MySQL server has gone away) raised in the yielded
# Messages.find_all_by_disposition in the main loop.
puts "#{Time.now} - ecod-poll - #{e}. Waiting 2
minutes.\n#{e.backtrace}"
sleep 120
# Disconnect and reconnect the DB connection.
# NOTE: A better way to do this has been suggested, but
# these lines are never reached, so I don't think they're the
# problem.
spec = ActiveRecord::Base.remove_connection
ActiveRecord::Base.establish_connection spec
retval = false
end
retval
end
# The yielded (and exception-raising) statement is in this loop:
loop do
newMessages = nil
success = catching_mysql_error do
newMessages = Message.find_all_by_disposition nil, :order =>
"senttime"
# Raises Mysql::Error
end
next if ! success
# ... further work on newMessages ...
end
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---