title: Mysql adapter ActiveRecord in Ruby throwing: 'Packets out of order'
body: <p>Ruby version: 1.9.1
  Mysql version: 5.0</p>
  
  <p>code written was:</p>
  
  <p><pre><code>
      require "rubygems"
      require "active_record"
      puts "1"
      class Source &lt; ActiveRecord::Base
          puts "2"
          establish_connection(
                  :adapter => "mysql",
                  :host => "localhost",
                  :username => "root",
                  :password => "root@123",
                  :database => "TrafficTracker"
              )
      end
      puts "3"
      traffic_source = Source.find(1)
      puts "4"
      puts traffic_source.source_name
  </pre></code></p>
  
  <p>Above code prints till 3 and breaks at Source.find(1) with error:</p>
  
  <blockquote>
    <p>1
    2
    3
    C:/Ruby/lib/ruby/1.9.1/mysql.rb:1019:in <code>read': Packets out of order: 0&lt;&gt;  (RuntimeError)<br>
    from C:/Ruby/lib/ruby/1.9.1/mysql.rb:444:in</code>read'
            from C:/Ruby/lib/ruby/1.9.1/mysql.rb:110:in <code>real_connect'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:600:in</code>connect'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:164:in <code>initialize'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:36:in</code>new'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:36:in <code>mysql_connection'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:228:in</code>new_connection'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in <code>checkout_new_connection'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in</code>block (2 levels) in c
    heckout'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco
    rd/connection_adapters/abstract/connection_pool.rb:186:in <code>loop'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco
    rd/connection_adapters/abstract/connection_pool.rb:186:in</code>block in checkout'
            from C:/Ruby/lib/ruby/1.9.1/monitor.rb:201:in <code>mon_synchronize' 
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco
    rd/connection_adapters/abstract/connection_pool.rb:185:in</code>checkout'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco
    rd/connection_adapters/abstract/connection_pool.rb:93:in <code>connection'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco
    rd/connection_adapters/abstract/connection_pool.rb:316:in</code>retrieve_connection'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco
    rd/connection_adapters/abstract/connection_specification.rb:97:in <code>retrieve_conn
    ection'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco
    rd/connection_adapters/abstract/connection_specification.rb:89:in</code>connection'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco
    rd/base.rb:679:in <code>columns'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/persistence.rb:284:in</code>attributes_from_column_definition'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/locking/optimistic.rb:62:in <code>attributes_from_column_definition'
            from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/base.rb:1394:in</code>initialize'
            from C:/WorkspaceGunjan/TrafficTracker/TrafficTracker/TestProject/RubyPrograms/RubyPrograms/AccessTrafficTracker.rb:22:in <code>new'
            from C:/WorkspaceGunjan/TrafficTracker/TrafficTracker/TestProject/RubyPrograms/RubyPrograms/AccessTrafficTracker.rb:22:in</code>'
    C:\WorkspaceGunjan\TrafficTracker\TrafficTracker\TestProject\RubyPrograms\RubyPrograms>
    C:\WorkspaceGunjan\TrafficTracker\TrafficTracker\TestProject\RubyPrograms\RubyPrograms>
    C:\WorkspaceGunjan\TrafficTracker\TrafficTracker\TestProject\RubyPrograms\RubyPrograms></p>
  </blockquote>
  
  <p>after analyzing more, i found problem with mysql.rb in \Ruby\lib\ruby\1.9.1
  Method creating problem is:
  <pre><code>
  def read()
        buf = []
        len = nil
        @sock.sync = false
        while len == nil or len == MAX_PACKET_LENGTH do
      a = @sock.read(4)
      len = a[0]+a[1]*256+a[2]*256*256
      pkt_nr = a[3]
      if @pkt_nr != pkt_nr then
        raise <strong>"Packets out of order: #{@pkt_nr}&lt;>#{pkt_nr}"</strong>   #Exception raised at line number 1019
      end
      @pkt_nr = @pkt_nr + 1 &amp; 0xff
      buf &lt;&lt; @sock.read(len)
        end
        @sock.sync = true
        buf.join
      end
  </pre></code></p>