This isn''t a Rails program, but since ActiveRecord is a part of Rails I thought it would be fitting to post here. Anyway, sorry if anyone is bothered by that. My problem is that I occasionally get this error: "Mysql::Error: MySQL server has gone away" with an ActiveRecord::StatementInvalid exception. I understand that what I need to do is make sure the connection is active before accessing the database, but I''m not sure the best way to do that. Here is my code at a glance: # some requires and some constants ActiveRecord::Base.establish_connection(dbconfig) # some code # start of a loop # some code Entry.create(:name => name_variable, :info => info_variable) # end It''s a very long-running script. Sometimes the loop takes 10 minutes, sometimes it takes several hours. I''ve come up with two possible solutions. One is to recreate the connection before every database activity with "ActiveRecord::Base.verify_active_connections!" so my code would look like this: # some code ActiveRecord::Base.verify_active_connections! Entry.create(:name => name_variable, :info => info_variable) # end The other solution is to rescue the ActiveRecord::StatementInvalid which I think would look something like this: # some code begin Entry.create(:name => name_variable, :info => info_variable) rescue ActiveRecord::StatementInvalid ActiveRecord::ConnectionAdapters::AbstractAdapter.reconnect! Entry.create(:name => name_variable, :info => info_variable) end # end But I have no idea if I''m doing that reconnect correctly. Any help on whether or not any one of these is correct, is either one better, or is there a better way than either of these? -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2008-Oct-03 18:07 UTC
Re: active record losing connection, in non-Rails script
On Oct 3, 2008, at 9:08 AM, James Dinkel wrote:> > This isn''t a Rails program, but since ActiveRecord is a part of > Rails I > thought it would be fitting to post here. Anyway, sorry if anyone is > bothered by that. > > My problem is that I occasionally get this error: "Mysql::Error: MySQL > server has gone away" with an ActiveRecord::StatementInvalid > exception. > I understand that what I need to do is make sure the connection is > active before accessing the database, but I''m not sure the best way to > do that. > > Here is my code at a glance: > > # some requires and some constants > ActiveRecord::Base.establish_connection(dbconfig) > # some code > # start of a loop > # some code > Entry.create(:name => name_variable, :info => info_variable) > # end > > It''s a very long-running script. Sometimes the loop takes 10 minutes, > sometimes it takes several hours. > > I''ve come up with two possible solutions. One is to recreate the > connection before every database activity with > "ActiveRecord::Base.verify_active_connections!" so my code would look > like this: > > # some code > ActiveRecord::Base.verify_active_connections! > Entry.create(:name => name_variable, :info => info_variable) > # end > > The other solution is to rescue the ActiveRecord::StatementInvalid > which > I think would look something like this: > > # some code > begin > Entry.create(:name => name_variable, :info => info_variable) > rescue ActiveRecord::StatementInvalid > ActiveRecord::ConnectionAdapters::AbstractAdapter.reconnect! > Entry.create(:name => name_variable, :info => info_variable) > end > # end > > But I have no idea if I''m doing that reconnect correctly. Any help on > whether or not any one of these is correct, is either one better, or > is > there a better way than either of these?In the past we did the latter, but instead of rescuing it just did it prior to any database statement after something that we knew was going to take awhile. These were mostly data migration scripts so we''d do a lot of munging, then ensure we were connected then stuff them all in. -philip --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
James Dinkel
2008-Oct-03 20:09 UTC
Re: active record losing connection, in non-Rails script
Philip Hallstrom wrote:> > In the past we did the latter, but instead of rescuing it just did it > prior to any database statement after something that we knew was going > to take awhile. These were mostly data migration scripts so we''d do a > lot of munging, then ensure we were connected then stuff them all in. > > -philipI think I _like_ the former better, so I think I''m going to give that a try and see if it works. I looked through the source, and it looks like ActiveRecord::Base.verify_active_connections! is just a wrapper that calls reconnect! on ALL connections but only if it''s necessary. Since I only have one connection and of course I want it to reconnect if necessary, so it sounds like this is probably my answer. I just don''t want to do it some way that''s going to get my code on thedailywtf.com. James -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---