ActiveRecordは、Threadを分けたら自分でコネクションを閉じないと駄目だった

| Comments

ある日から、作成中のアプリが

DEPRECATION WARNING: Database connections will not be closed automatically, please close your database connection at the end of the thread by calling `close` on your connection.  For example: ActiveRecord::Base.connection.close. (called from mon_synchronize at /home/mdcs/local/bin/ruby/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211)
DEPRECATION WARNING: Database connections will not be closed automatically, please close your database connection at the end of the thread by calling `close` on your connection.  For example: ActiveRecord::Base.connection.close. (called from mon_synchronize at /home/mdcs/local/bin/ruby/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211)
DEPRECATION WARNING: Database connections will not be closed automatically, please close your database connection at the end of the thread by calling `close` on your connection.  For example: ActiveRecord::Base.connection.close. (called from mon_synchronize at /home/mdcs/local/bin/ruby/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211)
DEPRECATION WARNING: Database connections will not be closed automatically, please close your database connection at the end of the thread by calling `close` on your connection.  For example: ActiveRecord::Base.connection.close. (called from mon_synchronize at /home/mdcs/local/bin/ruby/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211)
DEPRECATION WARNING: Database connections will not be closed automatically, please close your database connection at the end of the thread by calling `close` on your connection.  For example: ActiveRecord::Base.connection.close. (called from mon_synchronize at /home/mdcs/local/bin/ruby/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211)

等と、定期的にワーニングを出力するようになっていました。
また、その際にコネクションを全部リフレッシュするのか とっても遅くなる のです。

ぐぐってみても解らず、(´ε`;)ウーン… と、悩んでいましたが
ワーニングを見てみると、『スレッドを実行したら自分でコネクションを閉じてね。』 とまんまの答えが・・・

はい、閉じます。

Thread.new {
  begin
    block.call
  rescue => ex
    logger.info ex
  ensure
    ActiveRecord::Base.connection.close
  end
}

これは、恥ずかしい(;・∀・)
同じようなうっかりさんが、居ないことを願って・・・
余談ですが、rubyensureを使うのって余り無いのでトキメキました

Comments