[Question] Horizontal sharding in a thread


If you’re running in a separate thread, normally you would wrap any code accessing the database was ActiveRecord::Base.connection_pool.with_connection.

When you’re using horizontal sharding, does the ActiveRecord::Base.connected_to(shard: :foo, role: :reading) implicilty checkout a connection from the connection pool, or do you need to include the with_connection call?

If you do need to include the with_connection call, do you do that inside the connected_to block, or outside?

1 Like

Instead of wrapping with ActiveRecord::Base.connection_pool.with_connection, use Rails.application.executor.wrap and then inside of that use your ActiveRecord::Base.connected_to.