Skip to content

Commit

Permalink
Make sure we reset the connection_specification_name on
Browse files Browse the repository at this point in the history
remove_connection

When calling `remove_connection` on a model, we delete the pool so we also
need to reset the `connection_specification_name` so it will fallback to
the parent.
This was the current behavior before rails 5, which will fallback to the
parent connection pool.

[fixes #24959]

Special thanks to @jrafanie for working with me on this fix.
  • Loading branch information
arthurnn committed May 11, 2016
1 parent 932655a commit d6f3ad7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion activerecord/lib/active_record/connection_handling.rb
Expand Up @@ -95,7 +95,7 @@ def connection

# Return the specification name from the current class or its parent.
def connection_specification_name
unless defined?(@connection_specification_name)
if !defined?(@connection_specification_name) || @connection_specification_name.nil?
@connection_specification_name = self == Base ? "primary" : superclass.connection_specification_name
end
@connection_specification_name
Expand Down Expand Up @@ -133,6 +133,13 @@ def connected?
end

def remove_connection(name = connection_specification_name)
# if removing a connection that have a pool, we reset the
# connection_specification_name so it will use the parent
# pool.
if connection_handler.retrieve_connection_pool(name)
self.connection_specification_name = nil
end

connection_handler.remove_connection(name)
end

Expand Down
Expand Up @@ -89,6 +89,20 @@ def test_retrieve_connection_pool_copies_schema_cache_from_ancestor_pool
assert_equal @pool.schema_cache.size, Marshal.load(rd.read)
rd.close
end

def test_a_class_using_custom_pool_and_switching_back_to_primary
klass2 = Class.new(Base) { def self.name; 'klass2'; end }

assert_equal klass2.connection.object_id, ActiveRecord::Base.connection.object_id

pool = klass2.establish_connection(ActiveRecord::Base.connection_pool.spec.config)
assert_equal klass2.connection.object_id, pool.connection.object_id
refute_equal klass2.connection.object_id, ActiveRecord::Base.connection.object_id

klass2.remove_connection

assert_equal klass2.connection.object_id, ActiveRecord::Base.connection.object_id
end
end
end
end
Expand Down

2 comments on commit d6f3ad7

@prathamesh-sonpatki
Copy link
Member

@prathamesh-sonpatki prathamesh-sonpatki commented on d6f3ad7 May 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this introduced 2 random failures on PostgreSQL and MySQL adapters from test/cases/transaction_isolation_test.rb.

@arthurnn
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.