instance method setup_shared_connection_pool

Ruby on Rails 7.0.10

Since v6.0.6 Private

Available in: v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

setup_shared_connection_pool()

Shares the writing connection pool with connections on other handlers.

In an application with a primary and replica the test fixtures need to share a connection pool so that the reading connection can see data in the open transaction on the writing connection.

Source
# File activerecord/lib/active_record/test_fixtures.rb, line 202
      def setup_shared_connection_pool
        if ActiveRecord.legacy_connection_handling
          writing_handler = ActiveRecord::Base.connection_handlers[ActiveRecord.writing_role]

          ActiveRecord::Base.connection_handlers.values.each do |handler|
            if handler != writing_handler
              handler.connection_pool_names.each do |name|
                writing_pool_manager = writing_handler.send(:owner_to_pool_manager)[name]
                return unless writing_pool_manager

                pool_manager = handler.send(:owner_to_pool_manager)[name]
                @legacy_saved_pool_configs[handler][name] ||= {}
                pool_manager.shard_names.each do |shard_name|
                  writing_pool_config = writing_pool_manager.get_pool_config(nil, shard_name)
                  pool_config = pool_manager.get_pool_config(nil, shard_name)
                  next if pool_config == writing_pool_config

                  @legacy_saved_pool_configs[handler][name][shard_name] = pool_config
                  pool_manager.set_pool_config(nil, shard_name, writing_pool_config)
                end
              end
            end
          end
        else
          handler = ActiveRecord::Base.connection_handler

          handler.connection_pool_names.each do |name|
            pool_manager = handler.send(:owner_to_pool_manager)[name]
            pool_manager.shard_names.each do |shard_name|
              writing_pool_config = pool_manager.get_pool_config(ActiveRecord.writing_role, shard_name)
              @saved_pool_configs[name][shard_name] ||= {}
              pool_manager.role_names.each do |role|
                next unless pool_config = pool_manager.get_pool_config(role, shard_name)
                next if pool_config == writing_pool_config

                @saved_pool_configs[name][shard_name][role] = pool_config
                pool_manager.set_pool_config(role, shard_name, writing_pool_config)
              end
            end
          end
        end
      end

Defined in activerecord/lib/active_record/test_fixtures.rb line 202 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::TestFixtures

Type at least 2 characters to search.

↑↓ navigate · open · esc close