This project is archived and is in readonly mode.

#1293 ✓resolved
Esad Hajdarevic

MySQL adapter doesn't reconnect on lost connection

Reported by Esad Hajdarevic | October 30th, 2008 @ 01:22 AM | in 2.x

(copied from trac ticket #10277, looks like it's not yet fixed)

A connection to the MySQL server will time out after some time, depending on your server's wait_timeout and interactive_timeout settings, which are per default set to something like 8 days, so this problem will occur only in long running production environments.

The real problem is that when rails encounters the "Lost connection" error, it doesn't try to reconnect (weirdly enough, this behaviour was there in rails 0.13 but was kicked out for some reason!?). It remains unclear to me what LOST_CONNECTION_ERROR_MESSAGE constant in the mysql_adapter.rb is used for? (it is inthere as of AR-1.15.5).

This problem doesn't occur in development environment, as the dispatcher (see dispatcher.rb) calls ActiveRecord::Base.verify_active_connections! that takes care of this.

Could someone comment on this before I write/submit a patch? Why was the old behaviour kicked out? Why isn't Mysql#reconnect= used? (the reconnect behaviour is supported by the MySQL binding)

Comments and changes to this ticket

  • Cheah Chu Yeow

    Cheah Chu Yeow October 30th, 2008 @ 02:48 AM

    I've had this happen to me before in production on edge Rails awhile back. I'm not sure why the old behavior was kicked out though, but in practice it definitely doesn't reconnect all the time.

    And yup I can confirm that the LOST_CONNECTION_ERROR_MESSAGES constant is not being used anywhere in AR code.

  • Michael Koziarski

    Michael Koziarski October 30th, 2008 @ 10:38 AM

    The retry-magically code was removed as it was dangerous. You could easily end up resubmitting an insert statement and duplicating data.

    However the dispatcher in rails pre-verifies connections in production mode too, which version are you seeing this with?

  • Esad Hajdarevic

    Esad Hajdarevic October 31st, 2008 @ 02:56 AM

    This was in 2.1.2 - is the dispatcher pre-verifying connections 2.2 or is it already in 2.1.x?

  • Michael Koziarski

    Michael Koziarski November 1st, 2008 @ 11:58 AM

    The verification in 2.1 has an optimisation that you might be hitting:

    verify_active_connections in connection_specifications.rb uses a timeout. Make sure you haven't set that:

        cattr_accessor :verification_timeout, :instance_writer => false
        @@verification_timeout = 0

    But the logic is:

          # Lazily verify this connection, calling <tt>active?</tt> only if it hasn't
          # been called for +timeout+ seconds.
          def verify!(timeout)
            now =
            if (now - @last_verification) > timeout
              reconnect! unless active?
              @last_verification = now

    So that should be working fine. In 2.2 we remove that setting and just always verify the connection.

  • CancelProfileIsBroken

    CancelProfileIsBroken March 7th, 2009 @ 03:06 PM

    • State changed from “new” to “resolved”

    Optional reconnect: key added from #1838

  • bingbing

    bingbing March 26th, 2011 @ 02:05 AM

    • Importance changed from “” to “”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

Referenced by