【问题标题】:"No connection pool for UserActionEvent" when connecting to SqlServer连接到 SqlServer 时出现“没有 UserActionEvent 的连接池”
【发布时间】:2017-01-04 00:23:16
【问题描述】:

我有一个简单的 ruby​​ 脚本。
我需要连接 SqlServer 和 Mysql。我使用 gem 'mysql2' 连接成功,但我使用 gem 'tiny_tds' 和 'activerecord-sqlserver-adapter' 失败。 我的错误:

C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:570:in `retrieve_connection': No connection pool for UserActionEvent (ActiveRecord::ConnectionNotEstablished)
    from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activerecord-4.2.7.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
    from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activerecord-4.2.7.1/lib/active_record/connection_handling.rb:87:in `connection'
    from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activerecord-4.2.7.1/lib/active_record/querying.rb:39:in `find_by_sql'
    from D:/work/rails_obj/export_data/business/tear_bb.rb:49:in `main'
    from export_data.rb:22:in `main'
    from export_data.rb:28:in `<main>'

我的宝石文件:

gem 'mysql2', '0.4.4'
gem 'tiny_tds'
gem 'activerecord-sqlserver-adapter'
gem 'require_all', '1.3.3'
gem 'activerecord', '~>4.2.1'

我的 gemfile.lock:

GEM
  remote: https://gems.ruby-china.org/
  specs:
    activemodel (4.2.7.1)
      activesupport (= 4.2.7.1)
      builder (~> 3.1)
    activerecord (4.2.7.1)
      activemodel (= 4.2.7.1)
      activesupport (= 4.2.7.1)
      arel (~> 6.0)
    activerecord-sqlserver-adapter (4.2.15)
      activerecord (~> 4.2.1)
    activesupport (4.2.7.1)
      i18n (~> 0.7)
      json (~> 1.7, >= 1.7.7)
      minitest (~> 5.1)
      thread_safe (~> 0.3, >= 0.3.4)
      tzinfo (~> 1.1)
    arel (6.0.3)
    builder (3.2.2)
    i18n (0.7.0)
    json (1.8.3)
    mini_portile2 (2.1.0)
    minitest (5.9.0)
    mysql2 (0.4.4-x64-mingw32)
    require_all (1.3.3)
    thread_safe (0.3.5)
    tiny_tds (1.0.4-x64-mingw32)
      mini_portile2 (~> 2.0)
    tzinfo (1.2.2)
      thread_safe (~> 0.1)

PLATFORMS
  x64-mingw32

DEPENDENCIES
  activerecord (~> 4.2.1)
  activerecord-sqlserver-adapter
  mysql2 (= 0.4.4)
  require_all (= 1.3.3)
  tiny_tds

BUNDLED WITH
   1.12.5

我的代码:

class AnalyticsBase < ActiveRecord::Base
  db_config = YAML::load File.open 'configs/sqlserver.yml'
  establish_connection = db_config['Analytics']
  self.abstract_class = true
end

class UserActionEvent < AnalyticsBase
  self.table_name = 'UserActionEvent'
end

class TearBb
  def self.main
    sql3 = "select top 5 * from UserActionEvent"
    UserActionEvent.find_by_sql sql3
  end
end

TearBb.main

configs/sqlserver.yml:

comm_config: &comm
  adapter: sqlserver
  encoding: utf8
Analytics:
  <<: *comm
  host: myhost
  port: myport
  database: mydatabase
  username: myusername
  password: mypasword
  timeout: 60000

我的系统:Windows 7

我该如何处理这个问题? 谢谢!

【问题讨论】:

    标签: ruby-on-rails sql-server ruby activerecord


    【解决方案1】:

    不确定这里的用例是什么,但我认为您遇到的问题与establish_connection 方法的使用有关(来自ActiveRecord::Base,您应该将参数传递给它(而不是分配它) )

      establish_connection = db_config['Analytics']
    

    应该变成:

      establish_connection db_config['Analytics']
    

    【讨论】:

    • 感谢您的回答。我必须放弃这个问题。
    • 因为找不到原因!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    相关资源
    最近更新 更多