Aller au contenu

ORM call in async or concurrent context (ARCH6043)

Ce contenu n’est pas encore disponible dans votre langue.

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).

ids.each_slice(100).map do |slice|
Thread.new do
User.where(id: slice).update_all(active: true)
end
end.each(&:join)
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
end
end.each(&:join)

Wrap every ORM call inside a thread in ActiveRecord::Base.connection_pool.with_connection so the connection returns to the pool.