@@ -56,7 +56,7 @@ class ConnectionHandler
56
56
FINALIZER = lambda { |_ | ActiveSupport ::ForkTracker . check! }
57
57
private_constant :FINALIZER
58
58
59
- class StringConnectionOwner # :nodoc:
59
+ class StringConnectionName # :nodoc:
60
60
attr_reader :name
61
61
62
62
def initialize ( name )
@@ -73,8 +73,8 @@ def current_preventing_writes
73
73
end
74
74
75
75
def initialize
76
- # These caches are keyed by pool_config.connection_specification_name (PoolConfig#connection_specification_name ).
77
- @owner_to_pool_manager = Concurrent ::Map . new ( initial_capacity : 2 )
76
+ # These caches are keyed by pool_config.connection_name (PoolConfig#connection_name ).
77
+ @connection_name_to_pool_manager = Concurrent ::Map . new ( initial_capacity : 2 )
78
78
79
79
# Backup finalizer: if the forked child skipped Kernel#fork the early discard has not occurred
80
80
ObjectSpace . define_finalizer self , FINALIZER
@@ -89,24 +89,25 @@ def prevent_writes=(prevent_writes) # :nodoc:
89
89
end
90
90
91
91
def connection_pool_names # :nodoc:
92
- owner_to_pool_manager . keys
92
+ connection_name_to_pool_manager . keys
93
93
end
94
94
95
95
def all_connection_pools
96
- owner_to_pool_manager . values . flat_map { |m | m . pool_configs . map ( &:pool ) }
96
+ connection_name_to_pool_manager . values . flat_map { |m | m . pool_configs . map ( &:pool ) }
97
97
end
98
98
99
99
def connection_pool_list ( role = ActiveRecord ::Base . current_role )
100
- owner_to_pool_manager . values . flat_map { |m | m . pool_configs ( role ) . map ( &:pool ) }
100
+ connection_name_to_pool_manager . values . flat_map { |m | m . pool_configs ( role ) . map ( &:pool ) }
101
101
end
102
102
alias :connection_pools :connection_pool_list
103
103
104
104
def establish_connection ( config , owner_name : Base , role : ActiveRecord ::Base . current_role , shard : Base . current_shard )
105
- owner_name = StringConnectionOwner . new ( config . to_s ) if config . is_a? ( Symbol )
105
+ owner_name = StringConnectionName . new ( config . to_s ) if config . is_a? ( Symbol )
106
+
106
107
pool_config = resolve_pool_config ( config , owner_name , role , shard )
107
108
db_config = pool_config . db_config
108
109
109
- pool_manager = set_pool_manager ( pool_config . connection_specification_name )
110
+ pool_manager = set_pool_manager ( pool_config . connection_name )
110
111
111
112
# If there is an existing pool with the same values as the pool_config
112
113
# don't remove the connection. Connections should only be removed if we are
@@ -121,7 +122,7 @@ def establish_connection(config, owner_name: Base, role: ActiveRecord::Base.curr
121
122
pool_manager . set_pool_config ( role , shard , pool_config )
122
123
123
124
payload = {
124
- spec_name : pool_config . connection_specification_name ,
125
+ connection_name : pool_config . connection_name ,
125
126
shard : shard ,
126
127
config : db_config . configuration_hash
127
128
}
@@ -167,18 +168,18 @@ def flush_idle_connections!(role = ActiveRecord::Base.current_role)
167
168
# active or defined connection: if it is the latter, it will be
168
169
# opened and set as the active connection for the class it was defined
169
170
# for (not necessarily the current class).
170
- def retrieve_connection ( spec_name , role : ActiveRecord ::Base . current_role , shard : ActiveRecord ::Base . current_shard ) # :nodoc:
171
- pool = retrieve_connection_pool ( spec_name , role : role , shard : shard )
171
+ def retrieve_connection ( connection_name , role : ActiveRecord ::Base . current_role , shard : ActiveRecord ::Base . current_shard ) # :nodoc:
172
+ pool = retrieve_connection_pool ( connection_name , role : role , shard : shard )
172
173
173
174
unless pool
174
175
if shard != ActiveRecord ::Base . default_shard
175
- message = "No connection pool for '#{ spec_name } ' found for the '#{ shard } ' shard."
176
+ message = "No connection pool for '#{ connection_name } ' found for the '#{ shard } ' shard."
176
177
elsif ActiveRecord ::Base . connection_handler != ActiveRecord ::Base . default_connection_handler
177
- message = "No connection pool for '#{ spec_name } ' found for the '#{ ActiveRecord ::Base . current_role } ' role."
178
+ message = "No connection pool for '#{ connection_name } ' found for the '#{ ActiveRecord ::Base . current_role } ' role."
178
179
elsif role != ActiveRecord ::Base . default_role
179
- message = "No connection pool for '#{ spec_name } ' found for the '#{ role } ' role."
180
+ message = "No connection pool for '#{ connection_name } ' found for the '#{ role } ' role."
180
181
else
181
- message = "No connection pool for '#{ spec_name } ' found."
182
+ message = "No connection pool for '#{ connection_name } ' found."
182
183
end
183
184
184
185
raise ConnectionNotEstablished , message
@@ -189,36 +190,36 @@ def retrieve_connection(spec_name, role: ActiveRecord::Base.current_role, shard:
189
190
190
191
# Returns true if a connection that's accessible to this class has
191
192
# already been opened.
192
- def connected? ( spec_name , role : ActiveRecord ::Base . current_role , shard : ActiveRecord ::Base . current_shard )
193
- pool = retrieve_connection_pool ( spec_name , role : role , shard : shard )
193
+ def connected? ( connection_name , role : ActiveRecord ::Base . current_role , shard : ActiveRecord ::Base . current_shard )
194
+ pool = retrieve_connection_pool ( connection_name , role : role , shard : shard )
194
195
pool && pool . connected?
195
196
end
196
197
197
- def remove_connection_pool ( owner , role : ActiveRecord ::Base . current_role , shard : ActiveRecord ::Base . current_shard )
198
- if pool_manager = get_pool_manager ( owner )
198
+ def remove_connection_pool ( connection_name , role : ActiveRecord ::Base . current_role , shard : ActiveRecord ::Base . current_shard )
199
+ if pool_manager = get_pool_manager ( connection_name )
199
200
disconnect_pool_from_pool_manager ( pool_manager , role , shard )
200
201
end
201
202
end
202
203
203
- # Retrieving the connection pool happens a lot, so we cache it in @owner_to_pool_manager .
204
+ # Retrieving the connection pool happens a lot, so we cache it in @connection_name_to_pool_manager .
204
205
# This makes retrieving the connection pool O(1) once the process is warm.
205
206
# When a connection is established or removed, we invalidate the cache.
206
- def retrieve_connection_pool ( owner , role : ActiveRecord ::Base . current_role , shard : ActiveRecord ::Base . current_shard )
207
- pool_config = get_pool_manager ( owner ) &.get_pool_config ( role , shard )
207
+ def retrieve_connection_pool ( connection_name , role : ActiveRecord ::Base . current_role , shard : ActiveRecord ::Base . current_shard )
208
+ pool_config = get_pool_manager ( connection_name ) &.get_pool_config ( role , shard )
208
209
pool_config &.pool
209
210
end
210
211
211
212
private
212
- attr_reader :owner_to_pool_manager
213
+ attr_reader :connection_name_to_pool_manager
213
214
214
- # Returns the pool manager for an owner .
215
- def get_pool_manager ( owner )
216
- owner_to_pool_manager [ owner ]
215
+ # Returns the pool manager for a connection name / identifier .
216
+ def get_pool_manager ( connection_name )
217
+ connection_name_to_pool_manager [ connection_name ]
217
218
end
218
219
219
220
# 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
221
+ def set_pool_manager ( connection_name )
222
+ connection_name_to_pool_manager [ connection_name ] ||= PoolManager . new
222
223
end
223
224
224
225
def disconnect_pool_from_pool_manager ( pool_manager , role , shard )
@@ -240,7 +241,7 @@ def disconnect_pool_from_pool_manager(pool_manager, role, shard)
240
241
# pool_config.db_config.configuration_hash
241
242
# # => { host: "localhost", database: "foo", adapter: "sqlite3" }
242
243
#
243
- def resolve_pool_config ( config , owner_name , role , shard )
244
+ def resolve_pool_config ( config , connection_name , role , shard )
244
245
db_config = Base . configurations . resolve ( config )
245
246
246
247
raise ( AdapterNotSpecified , "database configuration does not specify adapter" ) unless db_config . adapter
@@ -270,7 +271,7 @@ def resolve_pool_config(config, owner_name, role, shard)
270
271
raise AdapterNotFound , "database configuration specifies nonexistent #{ db_config . adapter } adapter"
271
272
end
272
273
273
- ConnectionAdapters ::PoolConfig . new ( owner_name , db_config , role , shard )
274
+ ConnectionAdapters ::PoolConfig . new ( connection_name , db_config , role , shard )
274
275
end
275
276
end
276
277
end
0 commit comments