@@ -106,29 +106,29 @@ def establish_connection(config, owner_name: Base, role: ActiveRecord::Base.curr
106
106
pool_config = resolve_pool_config ( config , owner_name , role , shard )
107
107
db_config = pool_config . db_config
108
108
109
+ pool_manager = set_pool_manager ( pool_config . connection_specification_name )
110
+
109
111
# If there is an existing pool with the same values as the pool_config
110
112
# don't remove the connection. Connections should only be removed if we are
111
113
# establishing a connection on a class that is already connected to a different
112
114
# configuration.
113
- pool = retrieve_connection_pool ( pool_config . connection_specification_name , role : role , shard : shard )
114
-
115
- unless pool && pool . db_config == db_config
116
- remove_connection_pool ( pool_config . connection_specification_name , role : role , shard : shard )
115
+ existing_pool_config = pool_manager . get_pool_config ( role , shard )
117
116
118
- owner_to_pool_manager [ pool_config . connection_specification_name ] ||= PoolManager . new
119
- pool_manager = get_pool_manager ( pool_config . connection_specification_name )
117
+ if existing_pool_config && existing_pool_config . db_config == db_config
118
+ existing_pool_config . pool
119
+ else
120
+ disconnect_pool_from_pool_manager ( pool_manager , role , shard )
120
121
pool_manager . set_pool_config ( role , shard , pool_config )
121
- pool = pool_config . pool
122
- end
123
122
124
- payload = {
125
- spec_name : pool_config . connection_specification_name ,
126
- shard : shard ,
127
- config : db_config . configuration_hash
128
- }
123
+ payload = {
124
+ spec_name : pool_config . connection_specification_name ,
125
+ shard : shard ,
126
+ config : db_config . configuration_hash
127
+ }
129
128
130
- ActiveSupport ::Notifications . instrumenter . instrument ( "!connection.active_record" , payload ) do
131
- pool
129
+ ActiveSupport ::Notifications . instrumenter . instrument ( "!connection.active_record" , payload ) do
130
+ pool_config . pool
131
+ end
132
132
end
133
133
end
134
134
@@ -196,12 +196,7 @@ def connected?(spec_name, role: ActiveRecord::Base.current_role, shard: ActiveRe
196
196
197
197
def remove_connection_pool ( owner , role : ActiveRecord ::Base . current_role , shard : ActiveRecord ::Base . current_shard )
198
198
if pool_manager = get_pool_manager ( owner )
199
- pool_config = pool_manager . remove_pool_config ( role , shard )
200
-
201
- if pool_config
202
- pool_config . disconnect!
203
- pool_config . db_config
204
- end
199
+ disconnect_pool_from_pool_manager ( pool_manager , role , shard )
205
200
end
206
201
end
207
202
@@ -221,6 +216,20 @@ def get_pool_manager(owner)
221
216
owner_to_pool_manager [ owner ]
222
217
end
223
218
219
+ # Get the existing pool manager or initialize and assign a new one.
220
+ def set_pool_manager ( owner )
221
+ owner_to_pool_manager [ owner ] ||= PoolManager . new
222
+ end
223
+
224
+ def disconnect_pool_from_pool_manager ( pool_manager , role , shard )
225
+ pool_config = pool_manager . remove_pool_config ( role , shard )
226
+
227
+ if pool_config
228
+ pool_config . disconnect!
229
+ pool_config . db_config
230
+ end
231
+ end
232
+
224
233
# Returns an instance of PoolConfig for a given adapter.
225
234
# Accepts a hash one layer deep that contains all connection information.
226
235
#
0 commit comments