#184 new
Nick Dufresne

merb_activerecord does not verify active connections

Reported by Nick Dufresne | October 13th, 2008 @ 08:13 AM | in 1.0

if I run the following loop in the merb_activerecord#connect method i get zero connections showing after I run a query on the database.



Thread.new{ loop{ sleep(5); Merb.logger.debug("num of AR active connections from merb_ar: "+ ::ActiveRecord::Base.active_connections.length.to_s) } }

but if I instead add the line in the Merb::BootLoader.after_app_loads it will show 1 connection after I run a query against the database, but the merb_ar debug statement still shows 0 connections. The issue this seems to cause is that the Thread running from the merb_ar connect method



Thread.new{ loop{ sleep(60*60); ::ActiveRecord::Base.verify_active_connections! } }

never verifies any active connections because it doesn't find any. So the mysql connections get dropped (mysql server has gone away error) instead of staying alive. I began encountering this problem after moving to the merb-core 0.9.9.

Comments and changes to this ticket

  • Michael Klishin (antares)

    Michael Klishin (antares) October 13th, 2008 @ 03:49 PM

    • Assigned user set to “Michael Klishin (antares)”
    • Milestone set to 1.0
  • Steve Tooke (tooky)

    Steve Tooke (tooky) October 27th, 2008 @ 08:09 AM

    I'm also seeing this as a problem. I'm not sure how to go about writing a spec for this? Any ideas?

  • Steve Tooke (tooky)

    Steve Tooke (tooky) October 28th, 2008 @ 08:13 AM

    • Tag changed from activerecord, merb_activerecord, mysql to activerecord, merb_activerecord, mysql, patch

    Rails calls ActiveRecord::Base#verify_active_connections! before the action is dispatched.

    I've worked up a simple patch to merb_activerecord that mimics this behaviour. I'm not sure if this is the correct way to do this, so would apprectiate any comments.

    I also haven't written any specs, as I'm not sure how to go about specing this without using a mock? Again any comments would be great.

  • Michael Klishin (antares)

    Michael Klishin (antares) October 28th, 2008 @ 09:41 AM

    Steve,

    I am sorry but I don't feel like it the right way to go about it. It introduces overhead on every request. The real solution is to tune MySQL server configuration or spawn a thread that will ping MySQL every once in a while.

    See documentation chapter in MySQL reference manual.

  • Michael Klishin (antares)

    Michael Klishin (antares) October 28th, 2008 @ 09:44 AM

    And two more comments:

    1. Here is the wait_timeout variable documentation link, this is what you need to fix this issue.

    2. Mocks are usually strongly discouraged in Merb spec suite. Use them in your own application but not in the framework specs.

  • Nick Dufresne

    Nick Dufresne October 28th, 2008 @ 02:25 PM

    Good to see this issue is getting some attention,

    merb_activerecord currently has a spawned thread pinging MYSQL every once and a while, and that thread was keeping the connections alive, but now (as of Merb 0.9.8 i believe) for some reason that same thread does not seem to verify any of the active connections and therefore they die off if the mysql_timeout passes without any database calls. Like i mentioned above, im seeing connections from within my app (after_app_loads), but the thread spawned in the connection method of merb_activerecord sees none. Without getting at what caused this behavior to stop working (because I have no idea) a fix would be to wrap the thread in the BookLoader.after_app_loads block.

    @@@ Ruby

    #In Merb::Orms::ActiveRecord#connect

    Merb::BootLoader.after_app_loads do

    Thread.new{ loop{ sleep(60*60); ::ActiveRecord::Base.verify_active_connections! } }
    
    

    end

    
    
    That seems to fix the problem for me.  I have no idea how to spec this but I could create a patch for this if this seems like an acceptable solution.
    
  • Nick Dufresne

    Nick Dufresne October 28th, 2008 @ 02:28 PM

    sorry the formatting got messed up on that. Hopefully it works this time:

    
    
      #In Merb::Orms::ActiveRecord#connect
      Merb::BootLoader.after_app_loads do
        Thread.new{ loop{ sleep(60*60); ::ActiveRecord::Base.verify_active_connections! } }
      end
    
    
  • Steve Tooke (tooky)

    Steve Tooke (tooky) October 29th, 2008 @ 05:02 AM

    There's some discussion about this on the mailing list also: http://groups.google.com/group/m...

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

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

Shared Ticket Bins

Attachments

Pages