2012-03-09 80 views
0

運行:Mysql2 ::錯誤:關閉MySQL連接

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。

+0

這對我有效:在config/database.yml中,將':reconnect'更改爲'true',和':pool'到'20'。看到類似的問題:http://stackoverflow.com/questions/11773930/ruby-exception-occured-mysql2error-closed-mysql-connection/13714846#13714846 – 2012-12-05 01:22:14

回答

0

當然,你在Timeout.timeout(1)區塊做這個,我認爲你應該讓超時時間更長。

+0

它應該沒關係。在大多數情況下,Web應用程序會通過多個連接來訪問數據庫。相同的命令不會導致PgSql錯誤 – 2012-03-09 16:16:37

+0

但是,仍然引發超時錯誤。 – user973254 2012-03-09 16:19:31

0

它在MySql中給出錯誤但不是PgSql的原因是sleep是有效的Mysql函數,但不是有效的Pgsql函數。後者中的正確功能是pg_sleep。 也就是說,在Ruby代碼中,查詢會休眠2秒,因此會限制執行1秒,因此會引發超時異常。對於Pg的情況,它應該給出另一種例外,例如「睡眠不是有效的pgsql函數」