ORM call in async or concurrent context (ARCH6043)
Esta página aún no está disponible en tu idioma.
Key ruby.async.activerecord-in-thread · Severity warning · Category async
ActiveRecord checks out one connection per thread. Spawned threads that do not manage the connection explicitly drain the pool and never return their connections. Each thread should wrap its core work in with_connection (or checkout/checkin).
Incorrect
Section titled “Incorrect”ids.each_slice(100).map do |slice| Thread.new do User.where(id: slice).update_all(active: true) endend.each(&:join)Correct
Section titled “Correct”ids.each_slice(100).map do |slice| Thread.new do ActiveRecord::Base.connection_pool.with_connection do User.where(id: slice).update_all(active: true) end endend.each(&:join)Wrap every ORM call inside a thread in ActiveRecord::Base.connection_pool.with_connection so the connection returns to the pool.