提交 0e1625b1 编写于 作者: E Eileen M. Uchitelle 提交者: eileencodes

Merge pull request #36371 from eileencodes/move-schema-cache-to-pool

Move schema cache to pool
上级 388c14e5
......@@ -20,6 +20,26 @@ class ExclusiveConnectionTimeoutError < ConnectionTimeoutError
end
module ConnectionAdapters
module AbstractPool # :nodoc:
def get_schema_cache(connection)
@schema_cache ||= SchemaCache.new(connection)
@schema_cache.connection = connection
@schema_cache
end
def set_schema_cache(cache)
@schema_cache = cache
end
end
class NullPool # :nodoc:
include ConnectionAdapters::AbstractPool
def initialize
@schema_cache = nil
end
end
# Connection pool base class for managing Active Record database
# connections.
#
......@@ -336,6 +356,7 @@ def run
include MonitorMixin
include QueryCache::ConnectionPoolConfiguration
include ConnectionAdapters::AbstractPool
attr_accessor :automatic_reconnect, :checkout_timeout, :schema_cache
attr_reader :spec, :connections, :size, :reaper
......@@ -837,7 +858,6 @@ def remove_connection_from_thread_cache(conn, owner_thread = conn.owner)
def new_connection
Base.send(spec.adapter_method, spec.config).tap do |conn|
conn.schema_cache = schema_cache.dup if schema_cache
conn.check_version
end
end
......
......@@ -78,7 +78,7 @@ class AbstractAdapter
SIMPLE_INT = /\A\d+\z/
attr_accessor :pool
attr_reader :schema_cache, :visitor, :owner, :logger, :lock, :prepared_statements, :prevent_writes
attr_reader :visitor, :owner, :logger, :lock, :prepared_statements, :prevent_writes
alias :in_use? :owner
set_callback :checkin, :after, :enable_lazy_transactions!
......@@ -114,9 +114,8 @@ def initialize(connection, logger = nil, config = {}) # :nodoc:
@instrumenter = ActiveSupport::Notifications.instrumenter
@logger = logger
@config = config
@pool = nil
@pool = ActiveRecord::ConnectionAdapters::NullPool.new
@idle_since = Concurrent.monotonic_time
@schema_cache = SchemaCache.new self
@quoted_column_names, @quoted_table_names = {}, {}
@prevent_writes = false
@visitor = arel_visitor
......@@ -206,9 +205,13 @@ def lease
@owner = Thread.current
end
def schema_cache
@pool.get_schema_cache(self)
end
def schema_cache=(cache)
cache.connection = self
@schema_cache = cache
@pool.set_schema_cache(cache)
end
# this method must only be called while holding connection pool's mutex
......@@ -487,6 +490,9 @@ def discard!
#
# Prevent @connection's finalizer from touching the socket, or
# otherwise communicating with its server, when it is collected.
if schema_cache.connection == self
schema_cache.connection = nil
end
end
# Reset the state of this connection, directing the DBMS to clear
......
......@@ -109,6 +109,7 @@ def disconnect!
end
def discard! # :nodoc:
super
@connection.automatic_close = false
@connection = nil
end
......
......@@ -302,6 +302,7 @@ def disconnect!
end
def discard! # :nodoc:
super
@connection.socket_io.reopen(IO::NULL) rescue nil
@connection = nil
end
......
......@@ -134,7 +134,6 @@ class Railtie < Rails::Railtie # :nodoc:
cache = YAML.load(File.read(filename))
if cache.version == current_version
connection.schema_cache = cache
connection_pool.schema_cache = cache.dup
else
warn "Ignoring db/schema_cache.yml because it has expired. The current schema version is #{current_version}, but the one in the cache is #{cache.version}."
......
......@@ -507,7 +507,6 @@ def test_pool_sets_connection_schema_cache
pool.schema_cache = schema_cache
pool.with_connection do |conn|
assert_not_same pool.schema_cache, conn.schema_cache
assert_equal pool.schema_cache.size, conn.schema_cache.size
assert_same pool.schema_cache.columns(:posts), conn.schema_cache.columns(:posts)
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册