# Active record losing connection, in non-Rails script

**URL:** <https://www.ruby-forum.com/t/active-record-losing-connection-in-non-rails-script/150096>\
**Category:** Rails\
**Created:** [October 3, 2008, 6:08pm UTC](https://www.ruby-forum.com/t/active-record-losing-connection-in-non-rails-script/150096 "2008-10-03T18:08:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimdeuc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@jimdeuc](https://www.ruby-forum.com/u/jimdeuc)\
**Post date:** [October 3, 2008, 6:08pm UTC](https://www.ruby-forum.com/t/active-record-losing-connection-in-non-rails-script/150096/1 "2008-10-03T18:08:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jimdeuc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@jimdeuc](https://www.ruby-forum.com/u/jimdeuc)\
**Post date:** [October 3, 2008, 8:08pm UTC](https://www.ruby-forum.com/t/active-record-losing-connection-in-non-rails-script/150096/2 "2008-10-03T20:08:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jimdeuc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@jimdeuc](https://www.ruby-forum.com/u/jimdeuc)\
**Post date:** [October 3, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/active-record-losing-connection-in-non-rails-script/150096/3 "2008-10-03T22:09:57Z")

</div>

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](http://thedailywtf.com).

James
