2013-04-30 65 views
1

如果線程死亡或者我必須殺死一個正在使用ActiveRecord連接的線程,我如何確保ActiveRecord連接返回到池中?我不斷收到這樣的錯誤關閉死線程上的ActiveRecord連接

DEPRECATION WARNING: Database connections will not be closed automatically, please close your database connection at the end of the thread by calling接近on your connection. For example: ActiveRecord::Base.connection.close

但如何確保這種情況發生在該意外死亡,或一個調用Thread.kill上線?

回答

3

確保連接閉合=)

Thread.new do 
    begin 
    raise "foo" 
    ensure 
    begin 
     if (ActiveRecord::Base.connection && ActiveRecord::Base.connection.active?) 
      ActiveRecord::Base.connection.close 
     end 
     rescue 
     end 
    end 
end