Ruby抛出的Mysql适配器ActiveRecord:'数据包乱序'

时间:2011-02-03 16:27:52

标签: mysql ruby activerecord

Ruby版本:1.9.1 Mysql版本:5.0

编写的代码是:


    require "rubygems"
    require "active_record"
    puts "1"
    class Source < 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

上面的代码打印到3,并在Source.find(1)中断,错误:

  

1   2   3   C:/Ruby/lib/ruby/1.9.1/mysql.rb:1019:in read': Packets out of order: 0<> (RuntimeError)
from C:/Ruby/lib/ruby/1.9.1/mysql.rb:444:in
read'           来自C:/Ruby/lib/ruby/1.9.1/mysql.rb:110:in 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 connect'           来自C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:164:in 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 new'           来自C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:36:in 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 new_connection'           来自C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:在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块(2级)中C   heckout”           来自C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco   rd / connection_adapters / abstract / connection_pool.rb:186:在结账时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块中'           来自C:/Ruby/lib/ruby/1.9.1/monitor.rb:201:in 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 checkout'           来自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 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 retrieve_connection'           来自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 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 connection'           来自C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_reco   rd / base.rb:679:在columns' from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/persistence.rb:284:in attributes_from_column_definition'中           来自C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/locking/optimistic.rb:62:in 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 initialize'           来自C:/WorkspaceGunjan/TrafficTracker/TrafficTracker/TestProject/RubyPrograms/RubyPrograms/AccessTrafficTracker.rb:22:in new' from C:/WorkspaceGunjan/TrafficTracker/TrafficTracker/TestProject/RubyPrograms/RubyPrograms/AccessTrafficTracker.rb:22:in'   C:\ WorkspaceGunjan \ TrafficTracker \ TrafficTracker \ TestProject \ RubyPrograms \ RubyPrograms&GT;   C:\ WorkspaceGunjan \ TrafficTracker \ TrafficTracker \ TestProject \ RubyPrograms \ RubyPrograms&GT;   C:\ WorkspaceGunjan \ TrafficTracker \ TrafficTracker \ TestProject \ RubyPrograms \ RubyPrograms&GT;

在分析了更多之后,我在\ Ruby \ lib \ ruby​​ \ 1.9.1中发现了mysql.rb的问题 创建问题的方法是:


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 "Packets out of order: #{@pkt_nr}<>#{pkt_nr}"   #Exception raised at line number 1019
    end
    @pkt_nr = @pkt_nr + 1 & 0xff
    buf << @sock.read(len)
      end
      @sock.sync = true
      buf.join
    end

1 个答案:

答案 0 :(得分:0)

最后,在使用MySql和Postgresql完成所有工作后,我能够成功连接MySql。
工作集涉及:
Ruby版本:1.87
MySql版本:5.5
要使用ActiveRecord,需要安装适配器。在命令提示符下,写:
gem install mysql2。

在ActiveRecord层下构建MySql数据库与Ruby的成功连接的代码:


require "rubygems"
require "active_record"
class Source < ActiveRecord::Base
    set_table_name "sources"
    ActiveRecord::Base.establish_connection(
        :adapter => "mysql2",
        :host => "localhost",
        :username => "test",
        :password => "test",
        :port => 3306,
        :database => "TestDB",
        :socket => "mysql"
    )
end
source = Source.find(:first, :conditions => [ "source_id = ?", 2 ])
puts source.source_name

注意:使用的适配器是mysql2而不是mysql