# MySQL Losing Connection?

**URL:** <https://www.ruby-forum.com/t/mysql-losing-connection/82747>\
**Category:** Rails\
**Created:** [January 20, 2007, 8:52am UTC](https://www.ruby-forum.com/t/mysql-losing-connection/82747 "2007-01-20T08:52:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [January 20, 2007, 8:52am UTC](https://www.ruby-forum.com/t/mysql-losing-connection/82747/1 "2007-01-20T08:52:03Z")

</div>

I recently have been reading a Ruby On Rails book and have created a  
basic application. I went to test it, loaded up the server, but the  
script ran into some errors. Everytime a query is sent it says that the  
MySQL connection was lost when sending the query. I’m using the latest  
version of Ruby with MySQL 5.0 on Windows XP Pro. Can anyone help me  
resolve this problem. Thank you.

Cheers,  
Josh

---

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [January 20, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/mysql-losing-connection/82747/2 "2007-01-20T20:16:46Z")

</div>

Josh G. wrote:

> I recently have been reading a Ruby On Rails book and have created a  
> basic application. I went to test it, loaded up the server, but the  
> script ran into some errors. Everytime a query is sent it says that the  
> MySQL connection was lost when sending the query. I’m using the latest  
> version of Ruby with MySQL 5.0 on Windows XP Pro. Can anyone help me  
> resolve this problem. Thank you.
> 
> Cheers,  
> Josh

Can anyone help me with this? It’s rather urgent as I am supposed to  
have this project finished soon.

---

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [January 20, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/mysql-losing-connection/82747/3 "2007-01-20T21:21:28Z")

</div>

There are some known issues, I’ve commented on this before, check out  
the thread:  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/c07682cbfb2eef60/c3604401b504de96#c3604401b504de96](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/c07682cbfb2eef60/c3604401b504de96#c3604401b504de96)

---

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [January 21, 2007, 12:29am UTC](https://www.ruby-forum.com/t/mysql-losing-connection/82747/4 "2007-01-21T00:29:24Z")

</div>

Josh,

I was able to install the mySQL gem and that seemed to clear things up  
for me.

However, I feel like there was something else I did but I can’t remember  
what it was. I’m pretty sure I posted about it, but I can’t find the  
post.

Wes

---

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [January 20, 2007, 10:12pm UTC](https://www.ruby-forum.com/t/mysql-losing-connection/82747/5 "2007-01-20T22:12:24Z")

</div>

unknown wrote:

> There are some known issues, I’ve commented on this before, check out  
> the thread:  
> [http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/c07682cbfb2eef60/c3604401b504de96#c3604401b504de96](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/c07682cbfb2eef60/c3604401b504de96#c3604401b504de96)

Yea I saw that thread when I was searching and tried everything everyone  
suggested there but had no luck.

---

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [January 21, 2007, 1:00am UTC](https://www.ruby-forum.com/t/mysql-losing-connection/82747/6 "2007-01-21T01:00:50Z")

</div>

\*\* Warning total hack\*\*

You can actually get rid of this error by making a sleeper thread  
that wakes up and verifies the database connection. Add this to the  
bottom of your environment.rb file:

Thread.new{ loop{ sleep(60\*60);  
ActiveRecord::Base.verify\_active\_connections! } }.priority = -10

That will wake up one an hour and verify the active database  
connections. Adjust the time to suit your situation. Note that this  
only addresses the issue with the database connection timeout. There  
are some other things that can cause the loas connection error that  
this does not fix.

Cheers

– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [January 21, 2007, 12:34am UTC](https://www.ruby-forum.com/t/mysql-losing-connection/82747/7 "2007-01-21T00:34:49Z")

</div>

Even better, I wrote an email…

Do the following:

1. Run “gem install mysql” and choose the latest version for win32  
(should be 2.7.3)
2. Add the directory that contains libmySQL.dll to your PATH (C:\Program  
Files\MySQL\MySQL Server 5.0\bin)
3. Restart RadRails (obv. only if you’re running RadRails, you just need  
to be sure that the modified PATH is present when you start your Rails  
app.)

After I did this, I spent close to two hours working in the app, and saw  
no problems.

Potential Bonus: Supposedly, this driver is 10-15% faster than the pure  
Ruby one comes by default in Rails.

Wes

---

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [January 21, 2007, 7:13am UTC](https://www.ruby-forum.com/t/mysql-losing-connection/82747/8 "2007-01-21T07:13:43Z")

</div>

Awsome, didn’t know you could spark a thead in the enviroment, I’ll have  
some fun with that.

An intereting note, some of the newer MySQL ruby drivers have a  
reconnect paramiter.

Setting it to true will make the ruby reconnect after a disconnect.

I haven’t tried getting rails to use it yet though.  
Ezra Z. wrote:

> \*\* Warning total hack\*\*
> 
> You can actually get rid of this error by making a sleeper thread  
> that wakes up and verifies the database connection. Add this to the  
> bottom of your environment.rb file:
> 
> Thread.new{ loop{ sleep(60\*60);  
> ActiveRecord::Base.verify\_active\_connections! } }.priority = -10
> 
> That will wake up one an hour and verify the active database  
> connections. Adjust the time to suit your situation. Note that this  
> only addresses the issue with the database connection timeout. There  
> are some other things that can cause the loas connection error that  
> this does not fix.
> 
> Cheers
> 
> – Ezra Z.  
> – Lead Rails Evangelist  
> – removed\_email\_address@domain.invalid  
> – Engine Y., Serious Rails Hosting  
> – (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [December 10, 2007, 11:28pm UTC](https://www.ruby-forum.com/t/mysql-losing-connection/82747/9 "2007-12-10T23:28:18Z")

</div>

Hello all, I realize this is an old thread but I have been having the  
same problem here with a new project I am working on. Rails seems to  
lose its connection to the mysql database when the app sits idle for  
just a short time, like 5-10 minutes. I looked around and haven’t seen  
much else on this topic. I tried the sleeper thread trick, and it works  
if I set the timeout to 120 seconds. Obviously a solution like this  
doesn’t give me that warm feeling though.

My question now is: Is there any movement yet on being able to actually  
access the mysql reconnect setting from the rails/mongrel configuration  
somewhere, so that the app can just reconnect to mysql when it needs to?  
I’m really quite surprised that an issue like this would even be allowed  
into production applications written in Rails, yet people are using it  
all over. Is there an acceptable solution to this problem that doesn’t  
require a hack of the mysql driver code or a sleeper thread bandaid?

Thanks in advance,  
Lee

David H. wrote:

> Awsome, didn’t know you could spark a thead in the enviroment, I’ll have  
> some fun with that.
> 
> An intereting note, some of the newer MySQL ruby drivers have a  
> reconnect paramiter.
> 
> Setting it to true will make the ruby reconnect after a disconnect.
> 
> I haven’t tried getting rails to use it yet though.  
> Ezra Z. wrote:
> 
> > \*\* Warning total hack\*\*
> > 
> > You can actually get rid of this error by making a sleeper thread  
> > that wakes up and verifies the database connection. Add this to the  
> > bottom of your environment.rb file:
> > 
> > Thread.new{ loop{ sleep(60\*60);  
> > ActiveRecord::Base.verify\_active\_connections! } }.priority = -10
> > 
> > That will wake up one an hour and verify the active database  
> > connections. Adjust the time to suit your situation. Note that this  
> > only addresses the issue with the database connection timeout. There  
> > are some other things that can cause the loas connection error that  
> > this does not fix.
> > 
> > Cheers
> > 
> > – Ezra Z.  
> > – Lead Rails Evangelist  
> > – removed\_email\_address@domain.invalid  
> > – Engine Y., Serious Rails Hosting  
> > – (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [January 15, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/mysql-losing-connection/82747/10 "2008-01-15T17:43:31Z")

</div>

> My question now is: Is there any movement yet on being able to actually  
> access the mysql reconnect setting from the rails/mongrel configuration  
> somewhere, so that the app can just reconnect to mysql when it needs to?  
> I’m really quite surprised that an issue like this would even be allowed  
> into production applications written in Rails, yet people are using it  
> all over. Is there an acceptable solution to this problem that doesn’t  
> require a hack of the mysql driver code or a sleeper thread bandaid?
> 
> Thanks in advance,  
> Lee  
> Google query for rails how to set mysql to reconnect revealed this (if  
> helpful)  
> [http://wooga.drbacchus.com/rails-and-mysql-timeouts](http://wooga.drbacchus.com/rails-and-mysql-timeouts)
