Active record losing connection, in non-Rails script


#1

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?


#2

On Oct 3, 2008, at 9:08 AM, James D. wrote:

active before accessing the database, but I’m not sure the best way to

end

ActiveRecord::Base.verify_active_connections!
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


#3

Philip H. 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.

-philip

I 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