Mysql2 ::错误:关闭MySQL连接

时间:2012-03-09 15:51:33

标签: mysql ruby-on-rails ruby-on-rails-3 amazon-rds

运行:

Timeout.timeout(1) { User.find_by_sql('SELECT sleep(2) FROM users;') }

返回以下内容:

  User Load (1004.2ms)  SELECT sleep(2) FROM users;
ActiveRecord::StatementInvalid: : execution expired: SELECT sleep(2) FROM users;
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:687:in `query'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:687:in `block in exec_query'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:685:in `exec_query'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:679:in `select'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/base.rb:470:in `find_by_sql'
    from (irb):1:in `block in irb_binding'
    from /Users/Chris/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/timeout.rb:58:in `timeout'
    from (irb):1
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `

PgSql似乎没有发生以下情况。 Mysql2或ActiveRecord中是否有错误?

我已经读过,当连接太多,mysql无法处理时会发生这种情况。但是为什么PgSql不会发生这种情况呢?

PS。我正在使用Amazon RDS,如果这有所不同。

2 个答案:

答案 0 :(得分:0)

当然,你在Timeout.timeout(1)块中执行此操作,我认为你应该延长暂停时间。

答案 1 :(得分:0)

它在MySql而不是PgSql中出错的原因是sleep是一个有效的Mysql函数,但不是有效的Pgsql函数。后者的正确功能是pg_sleep。 也就是说,查询会休眠2秒,而在Ruby代码中,您将执行限制为1秒,因此会引发超时异常。对于Pg情况,它应该给出另一种例外,例如“睡眠不是有效的pgsql函数”