提交 303dca48 编写于 作者: R Rafael Mendonça França

Merge pull request #20175 from eugeneius/copy_schema_cache_after_fork

Add schema cache to new connection pool after fork
...@@ -933,7 +933,9 @@ def pool_for(owner) ...@@ -933,7 +933,9 @@ def pool_for(owner)
# A connection was established in an ancestor process that must have # A connection was established in an ancestor process that must have
# subsequently forked. We can't reuse the connection, but we can copy # subsequently forked. We can't reuse the connection, but we can copy
# the specification and establish a new connection with it. # the specification and establish a new connection with it.
establish_connection owner, ancestor_pool.spec establish_connection(owner, ancestor_pool.spec).tap do |pool|
pool.schema_cache = ancestor_pool.schema_cache if ancestor_pool.schema_cache
end
else else
owner_to_pool[owner.name] = nil owner_to_pool[owner.name] = nil
end end
......
...@@ -46,6 +46,52 @@ def test_retrieve_connection_pool_uses_superclass_pool_after_subclass_establish_ ...@@ -46,6 +46,52 @@ def test_retrieve_connection_pool_uses_superclass_pool_after_subclass_establish_
def test_connection_pools def test_connection_pools
assert_equal([@pool], @handler.connection_pools) assert_equal([@pool], @handler.connection_pools)
end end
if Process.respond_to?(:fork)
def test_connection_pool_per_pid
object_id = ActiveRecord::Base.connection.object_id
rd, wr = IO.pipe
rd.binmode
wr.binmode
pid = fork {
rd.close
wr.write Marshal.dump ActiveRecord::Base.connection.object_id
wr.close
exit!
}
wr.close
Process.waitpid pid
assert_not_equal object_id, Marshal.load(rd.read)
rd.close
end
def test_retrieve_connection_pool_copies_schema_cache_from_ancestor_pool
@pool.schema_cache = @pool.connection.schema_cache
@pool.schema_cache.add('posts')
rd, wr = IO.pipe
rd.binmode
wr.binmode
pid = fork {
rd.close
pool = @handler.retrieve_connection_pool(@klass)
wr.write Marshal.dump pool.schema_cache.size
wr.close
exit!
}
wr.close
Process.waitpid pid
assert_equal @pool.schema_cache.size, Marshal.load(rd.read)
rd.close
end
end
end end
end end
end end
...@@ -26,29 +26,6 @@ def setup ...@@ -26,29 +26,6 @@ def setup
assert ActiveRecord::Base.connection_handler.active_connections? assert ActiveRecord::Base.connection_handler.active_connections?
end end
if Process.respond_to?(:fork)
def test_connection_pool_per_pid
object_id = ActiveRecord::Base.connection.object_id
rd, wr = IO.pipe
rd.binmode
wr.binmode
pid = fork {
rd.close
wr.write Marshal.dump ActiveRecord::Base.connection.object_id
wr.close
exit!
}
wr.close
Process.waitpid pid
assert_not_equal object_id, Marshal.load(rd.read)
rd.close
end
end
def test_app_delegation def test_app_delegation
manager = ConnectionManagement.new(@app) manager = ConnectionManagement.new(@app)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册