Skip to content

Commit

Permalink
The Pool Config no longer keeps a reference to the connection class.
Browse files Browse the repository at this point in the history
Keeping a reference to the class caused subtle issues when combined with reloading in
development. Instead, cache the name and whether it's primary.

Fixes rails#54343.
  • Loading branch information
flavorjones committed Jan 24, 2025
1 parent 2ca006f commit c883a3f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
* The Pool Config no longer keeps a reference to the connection class.

Keeping a reference to the class caused subtle issues when combined with reloading in
development. Fixes #54343.

*Mike Dalessio*

* Fix SQL notifications sometimes not sent when using async queries.

```ruby
Expand Down
31 changes: 26 additions & 5 deletions activerecord/lib/active_record/connection_adapters/pool_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class PoolConfig # :nodoc:

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

def schema_reflection
@schema_reflection ||= SchemaReflection.new(db_config.lazy_schema_cache_path)
Expand All @@ -29,7 +28,7 @@ def disconnect_all!
def initialize(connection_class, db_config, role, shard)
super()
@server_version = nil
@connection_class = connection_class
self.connection_class = connection_class
@db_config = db_config
@role = role
@shard = shard
Expand All @@ -41,11 +40,33 @@ def server_version(connection)
@server_version || synchronize { @server_version ||= connection.get_database_version }
end

def connection_class=(connection_class)
case connection_class
when ConnectionHandler::StringConnectionName
@connection_class_name = connection_class
else
@connection_class_name = connection_class.name
end
@connection_class_primary_p = connection_class.primary_class?
end

def connection_class
case @connection_class_name
when ConnectionHandler::StringConnectionName
@connection_class_name
else
@connection_class_name.constantize
end
end

def connection_name
if connection_class.primary_class?
"ActiveRecord::Base"
return "ActiveRecord::Base" if @connection_class_primary_p

case @connection_class_name
when ConnectionHandler::StringConnectionName
@connection_class_name.name
else
connection_class.name
@connection_class_name
end
end

Expand Down

0 comments on commit c883a3f

Please sign in to comment.