2013-04-30 114 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