1

If a thread dies or I have to kill a thread that is using an ActiveRecord connection, how do I make sure that the ActiveRecord connection is returned back to the pool? I keep getting errors like

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

but how do I make sure this happens on a thread that dies unexpectedly, or one that call Thread.kill on?

4

1 回答 1

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
于 2013-07-26T00:04:56.747 回答