跳到内容 跳到搜索

Active Record 连接处理

方法
C
E
F
P
R
S
W

常量

DEFAULT_ENV = -> { RAILS_ENV.call || "default_env" }
 
RAILS_ENV = -> { (Rails.env if defined?(Rails.env)) || ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence }
 

属性

[W] connection_specification_name

实例公共方法

clear_active_connections!(role = nil)

# File activerecord/lib/active_record/connection_handling.rb, line 319
def clear_active_connections!(role = nil)
  deprecation_for_delegation(__method__)
  connection_handler.clear_active_connections!(role)
end

clear_all_connections!(role = nil)

# File activerecord/lib/active_record/connection_handling.rb, line 329
def clear_all_connections!(role = nil)
  deprecation_for_delegation(__method__)
  connection_handler.clear_all_connections!(role)
end

clear_query_caches_for_current_thread()

清除与当前线程关联的所有连接的查询缓存。

# File activerecord/lib/active_record/connection_handling.rb, line 244
def clear_query_caches_for_current_thread
  connection_handler.each_connection_pool do |pool|
    pool.connection.clear_query_cache if pool.active_connection?
  end
end

clear_reloadable_connections!(role = nil)

# File activerecord/lib/active_record/connection_handling.rb, line 324
def clear_reloadable_connections!(role = nil)
  deprecation_for_delegation(__method__)
  connection_handler.clear_reloadable_connections!(role)
end

connected?()

如果 Active Record 已连接,则返回 true

# File activerecord/lib/active_record/connection_handling.rb, line 291
def connected?
  connection_handler.connected?(connection_specification_name, role: current_role, shard: current_shard)
end

connected_to(role: nil, shard: nil, prevent_writes: false, &blk)

在块持续期间连接到角色(例如,写入、读取或自定义角色)和/或分片。在块的末尾,连接将返回到原始角色/分片。

如果只传递一个角色,Active Record 将根据请求的角色查找连接。如果请求了一个未建立的角色,则会引发 ActiveRecord::ConnectionNotEstablished 错误

ActiveRecord::Base.connected_to(role: :writing) do
  Dog.create! # creates dog using dog writing connection
end

ActiveRecord::Base.connected_to(role: :reading) do
  Dog.create! # throws exception because we're on a replica
end

在切换到分片时,也必须传递角色。如果传递了一个不存在的分片,则会引发 ActiveRecord::ConnectionNotEstablished 错误。

当传递分片和角色时,Active Record 将首先查找角色,然后按分片键查找连接。

ActiveRecord::Base.connected_to(role: :reading, shard: :shard_one_replica) do
  Dog.first # finds first Dog record stored on the shard one replica
end
# File activerecord/lib/active_record/connection_handling.rb, line 134
def connected_to(role: nil, shard: nil, prevent_writes: false, &blk)
  if self != Base && !abstract_class
    raise NotImplementedError, "calling `connected_to` is only allowed on ActiveRecord::Base or abstract classes."
  end

  if !connection_class? && !primary_class?
    raise NotImplementedError, "calling `connected_to` is only allowed on the abstract class that established the connection."
  end

  unless role || shard
    raise ArgumentError, "must provide a `shard` and/or `role`."
  end

  with_role_and_shard(role, shard, prevent_writes, &blk)
end

connected_to?(role:, shard: ActiveRecord::Base.default_shard)

如果角色是当前连接的角色和/或当前连接的分片,则返回 true。如果没有传递分片,则将使用默认分片。

ActiveRecord::Base.connected_to(role: :writing) do
  ActiveRecord::Base.connected_to?(role: :writing) #=> true
  ActiveRecord::Base.connected_to?(role: :reading) #=> false
end

ActiveRecord::Base.connected_to(role: :reading, shard: :shard_one) do
  ActiveRecord::Base.connected_to?(role: :reading, shard: :shard_one) #=> true
  ActiveRecord::Base.connected_to?(role: :reading, shard: :default) #=> false
  ActiveRecord::Base.connected_to?(role: :writing, shard: :shard_one) #=> true
end
# File activerecord/lib/active_record/connection_handling.rb, line 239
def connected_to?(role:, shard: ActiveRecord::Base.default_shard)
  current_role == role.to_sym && current_shard == shard.to_sym
end

connected_to_many(*classes, role:, shard: nil, prevent_writes: false)

将角色和/或分片连接到提供的连接名称。可以选择传递 prevent_writes 以阻止连接上的写入。reading 会自动将 prevent_writes 设置为 true。

connected_to_many 是深度嵌套 connected_to 块的替代方案。

用法

ActiveRecord::Base.connected_to_many(AnimalsRecord, MealsRecord, role: :reading) do
  Dog.first # Read from animals replica
  Dinner.first # Read from meals replica
  Person.first # Read from primary writer
end
# File activerecord/lib/active_record/connection_handling.rb, line 163
def connected_to_many(*classes, role:, shard: nil, prevent_writes: false)
  classes = classes.flatten

  if self != Base || classes.include?(Base)
    raise NotImplementedError, "connected_to_many can only be called on ActiveRecord::Base."
  end

  prevent_writes = true if role == ActiveRecord.reading_role

  append_to_connected_to_stack(role: role, shard: shard, prevent_writes: prevent_writes, klasses: classes)
  yield
ensure
  connected_to_stack.pop
end

connecting_to(role: default_role, shard: default_shard, prevent_writes: false)

使用指定连接。

此方法对于确保使用特定连接非常有用。例如,在只读模式下启动控制台时。

不建议在请求中使用此方法,因为它不会像 connected_to 一样让位于块。

# File activerecord/lib/active_record/connection_handling.rb, line 185
def connecting_to(role: default_role, shard: default_shard, prevent_writes: false)
  prevent_writes = true if role == ActiveRecord.reading_role

  append_to_connected_to_stack(role: role, shard: shard, prevent_writes: prevent_writes, klasses: [self])
end

connection()

返回当前与类关联的连接。这还可以用于“借用”连接来执行与任何特定 Active Record 无关的数据库工作。

# File activerecord/lib/active_record/connection_handling.rb, line 253
def connection
  retrieve_connection
end

connection_db_config()

返回关联连接的 db_config 对象

ActiveRecord::Base.connection_db_config
  #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10 @env_name="development",
    @name="primary", @config={pool: 5, timeout: 5000, database: "storage/development.sqlite3", adapter: "sqlite3"}>

仅用于读取。

# File activerecord/lib/active_record/connection_handling.rb, line 278
def connection_db_config
  connection_pool.db_config
end

connection_pool()

# File activerecord/lib/active_record/connection_handling.rb, line 282
def connection_pool
  connection_handler.retrieve_connection_pool(connection_specification_name, role: current_role, shard: current_shard) || raise(ConnectionNotEstablished)
end

connection_specification_name()

返回当前类或其父类的连接规范名称。

# File activerecord/lib/active_record/connection_handling.rb, line 260
def connection_specification_name
  if !defined?(@connection_specification_name) || @connection_specification_name.nil?
    return self == Base ? Base.name : superclass.connection_specification_name
  end
  @connection_specification_name
end

connects_to(database: {}, shards: {})

将模型连接到指定的数据库。database 关键字采用一个哈希,其中包含 roledatabase_key

这将使用 database_key 查找数据库配置,并建立与该配置的连接。

class AnimalsModel < ApplicationRecord
  self.abstract_class = true

  connects_to database: { writing: :primary, reading: :primary_replica }
end

connects_to 还支持水平分片。水平分片 API 也支持只读副本。您可以像这样将模型连接到分片列表

class AnimalsModel < ApplicationRecord
  self.abstract_class = true

  connects_to shards: {
    default: { writing: :primary, reading: :primary_replica },
    shard_two: { writing: :primary_shard_two, reading: :primary_shard_replica_two }
  }
end

返回一个数据库连接数组。

# File activerecord/lib/active_record/connection_handling.rb, line 81
def connects_to(database: {}, shards: {})
  raise NotImplementedError, "`connects_to` can only be called on ActiveRecord::Base or abstract classes" unless self == Base || abstract_class?

  if database.present? && shards.present?
    raise ArgumentError, "`connects_to` can only accept a `database` or `shards` argument, but not both arguments."
  end

  connections = []

  if shards.empty?
    shards[:default] = database
  end

  self.default_shard = shards.keys.first

  shards.each do |shard, database_keys|
    database_keys.each do |role, database_key|
      db_config = resolve_config_for_connection(database_key)

      self.connection_class = true
      connections << connection_handler.establish_connection(db_config, owner_name: self, role: role, shard: shard.to_sym)
    end
  end

  connections
end

establish_connection(config_or_env = nil)

建立与数据库的连接。接受一个哈希作为输入,其中必须指定 :adapter 键,并使用数据库适配器的名称(小写),例如常规数据库(MySQL、PostgreSQL 等)

ActiveRecord::Base.establish_connection(
  adapter:  "mysql2",
  host:     "localhost",
  username: "myuser",
  password: "mypass",
  database: "somedatabase"
)

SQLite 数据库示例

ActiveRecord::Base.establish_connection(
  adapter:  "sqlite3",
  database: "path/to/dbfile"
)

还接受字符串形式的键(例如,用于从 YAML 中解析)

ActiveRecord::Base.establish_connection(
  "adapter"  => "sqlite3",
  "database" => "path/to/dbfile"
)

或 URL

ActiveRecord::Base.establish_connection(
  "postgres://myuser:mypass@localhost/somedatabase"
)

如果设置了 ActiveRecord::Base.configurations(Rails 会自动将 config/database.yml 的内容加载到其中),还可以将符号作为参数提供,表示配置哈希中的键

ActiveRecord::Base.establish_connection(:production)

异常 AdapterNotSpecifiedAdapterNotFoundArgumentError 可能会在出现错误时返回。

# File activerecord/lib/active_record/connection_handling.rb, line 50
def establish_connection(config_or_env = nil)
  config_or_env ||= DEFAULT_ENV.call.to_sym
  db_config = resolve_config_for_connection(config_or_env)
  connection_handler.establish_connection(db_config, owner_name: self, role: current_role, shard: current_shard)
end

flush_idle_connections!(role = nil)

# File activerecord/lib/active_record/connection_handling.rb, line 334
def flush_idle_connections!(role = nil)
  deprecation_for_delegation(__method__)
  connection_handler.flush_idle_connections!(role)
end

prohibit_shard_swapping(enabled = true)

禁止在传入块内交换分片。

在某些情况下,您可能希望能够交换分片,但不允许嵌套调用 connected_toconnected_to_many 来再次交换。这在您使用分片来提供每个请求的数据库隔离时很有用。

# File activerecord/lib/active_record/connection_handling.rb, line 197
def prohibit_shard_swapping(enabled = true)
  prev_value = ActiveSupport::IsolatedExecutionState[:active_record_prohibit_shard_swapping]
  ActiveSupport::IsolatedExecutionState[:active_record_prohibit_shard_swapping] = enabled
  yield
ensure
  ActiveSupport::IsolatedExecutionState[:active_record_prohibit_shard_swapping] = prev_value
end

remove_connection(name = nil)

# File activerecord/lib/active_record/connection_handling.rb, line 295
    def remove_connection(name = nil)
      if name
        ActiveRecord.deprecator.warn(<<-MSG.squish)
          The name argument for `#remove_connection` is deprecated without replacement
          and will be removed in Rails 7.2. `#remove_connection` should always be called
          on the connection class directly, which makes the name argument obsolete.
        MSG
      end

      name ||= @connection_specification_name if defined?(@connection_specification_name)
      # if removing a connection that has a pool, we reset the
      # connection_specification_name so it will use the parent
      # pool.
      if connection_handler.retrieve_connection_pool(name, role: current_role, shard: current_shard)
        self.connection_specification_name = nil
      end

      connection_handler.remove_connection_pool(name, role: current_role, shard: current_shard)
    end

retrieve_connection()

# File activerecord/lib/active_record/connection_handling.rb, line 286
def retrieve_connection
  connection_handler.retrieve_connection(connection_specification_name, role: current_role, shard: current_shard)
end

shard_swapping_prohibited?()

确定当前是否禁止分片交换

# File activerecord/lib/active_record/connection_handling.rb, line 206
def shard_swapping_prohibited?
  ActiveSupport::IsolatedExecutionState[:active_record_prohibit_shard_swapping]
end

while_preventing_writes(enabled = true, &block)

无论角色如何,都禁止写入数据库。

在某些情况下,即使您位于可写入的数据库上,您也可能希望禁止写入数据库。while_preventing_writes 将在块持续期间禁止写入数据库。

此方法不提供与只读用户相同的保护,并且旨在防止意外写入。

有关此方法阻止的查询,请参见READ_QUERY

# File activerecord/lib/active_record/connection_handling.rb, line 221
def while_preventing_writes(enabled = true, &block)
  connected_to(role: current_role, prevent_writes: enabled, &block)
end