MySQL Losing Connection?

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

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.

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

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

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

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

** 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
[email protected]
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)

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

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
[email protected]
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)

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
[email protected]
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs