Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

MySQL adapter doesn't reconnect on lost connection

#1293

(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)

Reported by Esad Hajdarevic · October 30th, 2008 @ 01:22 AM

State: resolved
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Cheah Chu Yeow
    Cheah Chu Yeow

    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.

    October 30th, 2008 @ 02:48 AM

  2. Michael Koziarski
    Michael Koziarski

    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?

    October 30th, 2008 @ 10:38 AM

  3. Esad Hajdarevic
    Esad Hajdarevic

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

    October 31st, 2008 @ 02:56 AM

  4. Michael Koziarski
    Michael Koziarski

    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 = Time.now.to_i
            if (now - @last_verification) > timeout
              reconnect! unless active?
              @last_verification = now
            end
          end
    

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

    November 1st, 2008 @ 11:58 AM

  5. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from new to resolved

    March 7th, 2009 @ 03:06 PM

  6. bingbing