Sha256: fcfa1a0ced5f1c170c17225a28a487b4c37dbdf19e41a0d2390a4afe13d9d17a

Contents?: true

Size: 1.83 KB

Versions: 18

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module ConnectionAdapters
    class PoolConfig # :nodoc:
      include Mutex_m

      attr_reader :db_config, :role, :shard
      attr_writer :schema_reflection
      attr_accessor :connection_class

      def schema_reflection
        @schema_reflection ||= SchemaReflection.new(db_config.lazy_schema_cache_path)
      end

      INSTANCES = ObjectSpace::WeakMap.new
      private_constant :INSTANCES

      class << self
        def discard_pools!
          INSTANCES.each_key(&:discard_pool!)
        end

        def disconnect_all!
          INSTANCES.each_key { |c| c.disconnect!(automatic_reconnect: true) }
        end
      end

      def initialize(connection_class, db_config, role, shard)
        super()
        @connection_class = connection_class
        @db_config = db_config
        @role = role
        @shard = shard
        @pool = nil
        INSTANCES[self] = self
      end

      def connection_name
        if connection_class.primary_class?
          "ActiveRecord::Base"
        else
          connection_class.name
        end
      end

      def disconnect!(automatic_reconnect: false)
        ActiveSupport::ForkTracker.check!

        return unless @pool

        synchronize do
          return unless @pool

          @pool.automatic_reconnect = automatic_reconnect
          @pool.disconnect!
        end

        nil
      end

      def pool
        ActiveSupport::ForkTracker.check!

        @pool || synchronize { @pool ||= ConnectionAdapters::ConnectionPool.new(self) }
      end

      def discard_pool!
        return unless @pool

        synchronize do
          return unless @pool

          @pool.discard!
          @pool = nil
        end
      end
    end
  end
end

ActiveSupport::ForkTracker.after_fork { ActiveRecord::ConnectionAdapters::PoolConfig.discard_pools! }

Version data entries

18 entries across 18 versions & 4 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.5.1 lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.5 lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.4.2 lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.4.1 lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.4 lib/active_record/connection_adapters/pool_config.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/connection_adapters/pool_config.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.3.4 lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.3.2 lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.3.1 lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.3 lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.2 lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.1 lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.0 lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.0.rc2 lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.0.rc1 lib/active_record/connection_adapters/pool_config.rb
activerecord-7.1.0.beta1 lib/active_record/connection_adapters/pool_config.rb