activerecord 8.0.2 → 8.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +555 -388
- data/README.rdoc +2 -2
- data/lib/active_record/association_relation.rb +1 -1
- data/lib/active_record/associations/association.rb +1 -1
- data/lib/active_record/associations/belongs_to_association.rb +11 -1
- data/lib/active_record/associations/builder/association.rb +16 -5
- data/lib/active_record/associations/builder/belongs_to.rb +17 -4
- data/lib/active_record/associations/builder/collection_association.rb +7 -3
- data/lib/active_record/associations/builder/has_one.rb +1 -1
- data/lib/active_record/associations/builder/singular_association.rb +33 -5
- data/lib/active_record/associations/collection_association.rb +3 -3
- data/lib/active_record/associations/collection_proxy.rb +22 -4
- data/lib/active_record/associations/deprecation.rb +88 -0
- data/lib/active_record/associations/errors.rb +3 -0
- data/lib/active_record/associations/join_dependency.rb +2 -0
- data/lib/active_record/associations/preloader/branch.rb +1 -0
- data/lib/active_record/associations.rb +159 -21
- data/lib/active_record/attribute_methods/query.rb +34 -0
- data/lib/active_record/attribute_methods/serialization.rb +17 -4
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +10 -2
- data/lib/active_record/attribute_methods.rb +1 -1
- data/lib/active_record/attributes.rb +38 -24
- data/lib/active_record/autosave_association.rb +2 -2
- data/lib/active_record/base.rb +2 -4
- data/lib/active_record/coders/json.rb +14 -5
- data/lib/active_record/connection_adapters/abstract/connection_handler.rb +2 -4
- data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +16 -3
- data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +51 -12
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +413 -76
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +55 -40
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +36 -9
- data/lib/active_record/connection_adapters/abstract/quoting.rb +15 -24
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +7 -2
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +26 -34
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +2 -1
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +89 -23
- data/lib/active_record/connection_adapters/abstract/transaction.rb +25 -3
- data/lib/active_record/connection_adapters/abstract_adapter.rb +89 -21
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +43 -13
- data/lib/active_record/connection_adapters/column.rb +17 -4
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +4 -4
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +2 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +42 -5
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +26 -4
- data/lib/active_record/connection_adapters/mysql2/database_statements.rb +27 -22
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +1 -2
- data/lib/active_record/connection_adapters/postgresql/column.rb +4 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +26 -17
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +21 -10
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +8 -6
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +8 -21
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +67 -31
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +81 -48
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +25 -9
- data/lib/active_record/connection_adapters/schema_cache.rb +2 -2
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +39 -27
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +0 -8
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +4 -13
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +58 -34
- data/lib/active_record/connection_adapters/trilogy/database_statements.rb +4 -3
- data/lib/active_record/connection_adapters/trilogy_adapter.rb +1 -1
- data/lib/active_record/connection_adapters.rb +1 -0
- data/lib/active_record/connection_handling.rb +15 -10
- data/lib/active_record/core.rb +13 -10
- data/lib/active_record/counter_cache.rb +33 -8
- data/lib/active_record/database_configurations/database_config.rb +5 -1
- data/lib/active_record/database_configurations/hash_config.rb +59 -9
- data/lib/active_record/database_configurations/url_config.rb +13 -3
- data/lib/active_record/database_configurations.rb +7 -3
- data/lib/active_record/delegated_type.rb +2 -2
- data/lib/active_record/dynamic_matchers.rb +54 -69
- data/lib/active_record/encryption/encryptable_record.rb +5 -5
- data/lib/active_record/encryption/encrypted_attribute_type.rb +2 -2
- data/lib/active_record/encryption/encryptor.rb +39 -25
- data/lib/active_record/encryption/scheme.rb +1 -1
- data/lib/active_record/enum.rb +37 -20
- data/lib/active_record/errors.rb +23 -7
- data/lib/active_record/explain.rb +1 -1
- data/lib/active_record/explain_registry.rb +51 -2
- data/lib/active_record/filter_attribute_handler.rb +73 -0
- data/lib/active_record/fixture_set/table_row.rb +19 -2
- data/lib/active_record/fixtures.rb +2 -2
- data/lib/active_record/gem_version.rb +2 -2
- data/lib/active_record/inheritance.rb +1 -1
- data/lib/active_record/insert_all.rb +12 -7
- data/lib/active_record/locking/optimistic.rb +7 -0
- data/lib/active_record/locking/pessimistic.rb +5 -0
- data/lib/active_record/log_subscriber.rb +2 -6
- data/lib/active_record/middleware/shard_selector.rb +34 -17
- data/lib/active_record/migration/command_recorder.rb +14 -1
- data/lib/active_record/migration/compatibility.rb +34 -24
- data/lib/active_record/migration/default_schema_versions_formatter.rb +30 -0
- data/lib/active_record/migration.rb +31 -21
- data/lib/active_record/model_schema.rb +36 -10
- data/lib/active_record/nested_attributes.rb +2 -0
- data/lib/active_record/persistence.rb +34 -3
- data/lib/active_record/query_cache.rb +22 -15
- data/lib/active_record/query_logs.rb +7 -7
- data/lib/active_record/querying.rb +4 -4
- data/lib/active_record/railtie.rb +34 -5
- data/lib/active_record/railties/controller_runtime.rb +11 -6
- data/lib/active_record/railties/databases.rake +23 -19
- data/lib/active_record/railties/job_checkpoints.rb +15 -0
- data/lib/active_record/railties/job_runtime.rb +10 -11
- data/lib/active_record/reflection.rb +35 -0
- data/lib/active_record/relation/batches.rb +25 -11
- data/lib/active_record/relation/calculations.rb +35 -25
- data/lib/active_record/relation/delegation.rb +0 -1
- data/lib/active_record/relation/finder_methods.rb +41 -24
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +9 -9
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +7 -7
- data/lib/active_record/relation/predicate_builder.rb +9 -7
- data/lib/active_record/relation/query_attribute.rb +3 -1
- data/lib/active_record/relation/query_methods.rb +44 -33
- data/lib/active_record/relation/spawn_methods.rb +6 -6
- data/lib/active_record/relation/where_clause.rb +9 -10
- data/lib/active_record/relation.rb +38 -16
- data/lib/active_record/result.rb +44 -21
- data/lib/active_record/runtime_registry.rb +41 -58
- data/lib/active_record/sanitization.rb +2 -0
- data/lib/active_record/schema_dumper.rb +12 -10
- data/lib/active_record/scoping.rb +0 -1
- data/lib/active_record/secure_token.rb +3 -3
- data/lib/active_record/signed_id.rb +46 -18
- data/lib/active_record/statement_cache.rb +13 -9
- data/lib/active_record/store.rb +44 -19
- data/lib/active_record/structured_event_subscriber.rb +85 -0
- data/lib/active_record/table_metadata.rb +5 -20
- data/lib/active_record/tasks/abstract_tasks.rb +76 -0
- data/lib/active_record/tasks/database_tasks.rb +44 -45
- data/lib/active_record/tasks/mysql_database_tasks.rb +3 -40
- data/lib/active_record/tasks/postgresql_database_tasks.rb +14 -40
- data/lib/active_record/tasks/sqlite_database_tasks.rb +14 -26
- data/lib/active_record/test_databases.rb +14 -4
- data/lib/active_record/test_fixtures.rb +27 -2
- data/lib/active_record/testing/query_assertions.rb +8 -2
- data/lib/active_record/timestamp.rb +4 -2
- data/lib/active_record/transaction.rb +2 -5
- data/lib/active_record/transactions.rb +34 -10
- data/lib/active_record/type/hash_lookup_type_map.rb +2 -1
- data/lib/active_record/type/internal/timezone.rb +7 -0
- data/lib/active_record/type/json.rb +15 -2
- data/lib/active_record/type/serialized.rb +11 -4
- data/lib/active_record/type/type_map.rb +1 -1
- data/lib/active_record/type_caster/connection.rb +2 -1
- data/lib/active_record/validations/associated.rb +1 -1
- data/lib/active_record.rb +68 -5
- data/lib/arel/alias_predication.rb +2 -0
- data/lib/arel/crud.rb +8 -11
- data/lib/arel/delete_manager.rb +5 -0
- data/lib/arel/nodes/count.rb +2 -2
- data/lib/arel/nodes/delete_statement.rb +4 -2
- data/lib/arel/nodes/function.rb +4 -10
- data/lib/arel/nodes/named_function.rb +2 -2
- data/lib/arel/nodes/node.rb +1 -1
- data/lib/arel/nodes/update_statement.rb +4 -2
- data/lib/arel/nodes.rb +0 -2
- data/lib/arel/select_manager.rb +13 -4
- data/lib/arel/update_manager.rb +5 -0
- data/lib/arel/visitors/dot.rb +2 -3
- data/lib/arel/visitors/postgresql.rb +55 -0
- data/lib/arel/visitors/sqlite.rb +55 -8
- data/lib/arel/visitors/to_sql.rb +5 -21
- data/lib/arel.rb +3 -1
- data/lib/rails/generators/active_record/application_record/USAGE +1 -1
- metadata +16 -12
- data/lib/active_record/explain_subscriber.rb +0 -34
- data/lib/active_record/normalization.rb +0 -163
|
@@ -8,18 +8,13 @@ require "active_record/connection_adapters/abstract/connection_pool/reaper"
|
|
|
8
8
|
|
|
9
9
|
module ActiveRecord
|
|
10
10
|
module ConnectionAdapters
|
|
11
|
-
module AbstractPool # :nodoc:
|
|
12
|
-
end
|
|
13
|
-
|
|
14
11
|
class NullPool # :nodoc:
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
class NullConfig # :nodoc:
|
|
12
|
+
class NullConfig
|
|
18
13
|
def method_missing(...)
|
|
19
14
|
nil
|
|
20
15
|
end
|
|
21
16
|
end
|
|
22
|
-
NULL_CONFIG = NullConfig.new
|
|
17
|
+
NULL_CONFIG = NullConfig.new
|
|
23
18
|
|
|
24
19
|
def initialize
|
|
25
20
|
super()
|
|
@@ -36,6 +31,7 @@ module ActiveRecord
|
|
|
36
31
|
end
|
|
37
32
|
|
|
38
33
|
def schema_cache; end
|
|
34
|
+
def query_cache; end
|
|
39
35
|
def connection_descriptor; end
|
|
40
36
|
def checkin(_); end
|
|
41
37
|
def remove(_); end
|
|
@@ -48,6 +44,11 @@ module ActiveRecord
|
|
|
48
44
|
def dirties_query_cache
|
|
49
45
|
true
|
|
50
46
|
end
|
|
47
|
+
|
|
48
|
+
def pool_transaction_isolation_level; end
|
|
49
|
+
def pool_transaction_isolation_level=(isolation_level)
|
|
50
|
+
raise NotImplementedError, "This method should never be called"
|
|
51
|
+
end
|
|
51
52
|
end
|
|
52
53
|
|
|
53
54
|
# = Active Record Connection Pool
|
|
@@ -100,13 +101,21 @@ module ActiveRecord
|
|
|
100
101
|
# There are several connection-pooling-related options that you can add to
|
|
101
102
|
# your database connection configuration:
|
|
102
103
|
#
|
|
103
|
-
# * +pool+: maximum number of connections the pool may manage (default 5).
|
|
104
|
-
# * +idle_timeout+: number of seconds that a connection will be kept
|
|
105
|
-
# unused in the pool before it is automatically disconnected (default
|
|
106
|
-
# 300 seconds). Set this to zero to keep connections forever.
|
|
107
104
|
# * +checkout_timeout+: number of seconds to wait for a connection to
|
|
108
105
|
# become available before giving up and raising a timeout error (default
|
|
109
106
|
# 5 seconds).
|
|
107
|
+
# * +idle_timeout+: number of seconds that a connection will be kept
|
|
108
|
+
# unused in the pool before it is automatically disconnected (default
|
|
109
|
+
# 300 seconds). Set this to zero to keep connections forever.
|
|
110
|
+
# * +keepalive+: number of seconds between keepalive checks if the
|
|
111
|
+
# connection has been idle (default 600 seconds).
|
|
112
|
+
# * +max_age+: number of seconds the pool will allow the connection to
|
|
113
|
+
# exist before retiring it at next checkin. (default Float::INFINITY).
|
|
114
|
+
# * +max_connections+: maximum number of connections the pool may manage (default 5).
|
|
115
|
+
# Set to +nil+ or -1 for unlimited connections.
|
|
116
|
+
# * +min_connections+: minimum number of connections the pool will open and maintain (default 0).
|
|
117
|
+
# * +pool_jitter+: maximum reduction factor to apply to +max_age+ and
|
|
118
|
+
# +keepalive+ intervals (default 0.2; range 0.0-1.0).
|
|
110
119
|
#
|
|
111
120
|
#--
|
|
112
121
|
# Synchronization policy:
|
|
@@ -114,6 +123,7 @@ module ActiveRecord
|
|
|
114
123
|
# * access to these instance variables needs to be in +synchronize+:
|
|
115
124
|
# * @connections
|
|
116
125
|
# * @now_connecting
|
|
126
|
+
# * @maintaining
|
|
117
127
|
# * private methods that require being called in a +synchronize+ blocks
|
|
118
128
|
# are now explicitly documented
|
|
119
129
|
class ConnectionPool
|
|
@@ -124,7 +134,7 @@ module ActiveRecord
|
|
|
124
134
|
else
|
|
125
135
|
class WeakThreadKeyMap # :nodoc:
|
|
126
136
|
# FIXME: On 3.3 we could use ObjectSpace::WeakKeyMap
|
|
127
|
-
# but it currently
|
|
137
|
+
# but it currently causes GC crashes: https://github.com/byroot/rails/pull/3
|
|
128
138
|
def initialize
|
|
129
139
|
@map = {}
|
|
130
140
|
end
|
|
@@ -170,21 +180,30 @@ module ActiveRecord
|
|
|
170
180
|
end
|
|
171
181
|
end
|
|
172
182
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
183
|
+
if RUBY_ENGINE == "ruby"
|
|
184
|
+
# Thanks to the GVL, the LeaseRegistry doesn't need to be synchronized on MRI
|
|
185
|
+
class LeaseRegistry < WeakThreadKeyMap # :nodoc:
|
|
186
|
+
def [](context)
|
|
187
|
+
super || (self[context] = Lease.new)
|
|
188
|
+
end
|
|
177
189
|
end
|
|
190
|
+
else
|
|
191
|
+
class LeaseRegistry # :nodoc:
|
|
192
|
+
def initialize
|
|
193
|
+
@mutex = Mutex.new
|
|
194
|
+
@map = WeakThreadKeyMap.new
|
|
195
|
+
end
|
|
178
196
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
197
|
+
def [](context)
|
|
198
|
+
@mutex.synchronize do
|
|
199
|
+
@map[context] ||= Lease.new
|
|
200
|
+
end
|
|
182
201
|
end
|
|
183
|
-
end
|
|
184
202
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
203
|
+
def clear
|
|
204
|
+
@mutex.synchronize do
|
|
205
|
+
@map.clear
|
|
206
|
+
end
|
|
188
207
|
end
|
|
189
208
|
end
|
|
190
209
|
end
|
|
@@ -216,10 +235,10 @@ module ActiveRecord
|
|
|
216
235
|
|
|
217
236
|
include MonitorMixin
|
|
218
237
|
prepend QueryCache::ConnectionPoolConfiguration
|
|
219
|
-
include ConnectionAdapters::AbstractPool
|
|
220
238
|
|
|
221
239
|
attr_accessor :automatic_reconnect, :checkout_timeout
|
|
222
|
-
attr_reader :db_config, :
|
|
240
|
+
attr_reader :db_config, :max_connections, :min_connections, :max_age, :keepalive, :reaper, :pool_config, :async_executor, :role, :shard
|
|
241
|
+
alias :size :max_connections
|
|
223
242
|
|
|
224
243
|
delegate :schema_reflection, :server_version, to: :pool_config
|
|
225
244
|
|
|
@@ -239,7 +258,10 @@ module ActiveRecord
|
|
|
239
258
|
|
|
240
259
|
@checkout_timeout = db_config.checkout_timeout
|
|
241
260
|
@idle_timeout = db_config.idle_timeout
|
|
242
|
-
@
|
|
261
|
+
@max_connections = db_config.max_connections
|
|
262
|
+
@min_connections = db_config.min_connections
|
|
263
|
+
@max_age = db_config.max_age
|
|
264
|
+
@keepalive = db_config.keepalive
|
|
243
265
|
|
|
244
266
|
# This variable tracks the cache of threads mapped to reserved connections, with the
|
|
245
267
|
# sole purpose of speeding up the +connection+ method. It is not the authoritative
|
|
@@ -261,6 +283,12 @@ module ActiveRecord
|
|
|
261
283
|
# currently in the process of independently establishing connections to the DB.
|
|
262
284
|
@now_connecting = 0
|
|
263
285
|
|
|
286
|
+
# Sometimes otherwise-idle connections are temporarily held by the Reaper for
|
|
287
|
+
# maintenance. This variable tracks the number of connections currently in that
|
|
288
|
+
# state -- if a thread requests a connection and there are none available, it
|
|
289
|
+
# will await any in-maintenance connections in preference to creating a new one.
|
|
290
|
+
@maintaining = 0
|
|
291
|
+
|
|
264
292
|
@threads_blocking_new_connections = 0
|
|
265
293
|
|
|
266
294
|
@available = ConnectionLeasingQueue.new self
|
|
@@ -271,13 +299,17 @@ module ActiveRecord
|
|
|
271
299
|
|
|
272
300
|
@schema_cache = nil
|
|
273
301
|
|
|
302
|
+
@activated = false
|
|
303
|
+
@original_context = ActiveSupport::IsolatedExecutionState.context
|
|
304
|
+
|
|
305
|
+
@reaper_lock = Monitor.new
|
|
274
306
|
@reaper = Reaper.new(self, db_config.reaping_frequency)
|
|
275
307
|
@reaper.run
|
|
276
308
|
end
|
|
277
309
|
|
|
278
310
|
def inspect # :nodoc:
|
|
279
|
-
name_field = " name=#{
|
|
280
|
-
shard_field = " shard=#{
|
|
311
|
+
name_field = " name=#{name_inspect}" if name_inspect
|
|
312
|
+
shard_field = " shard=#{shard_inspect}" if shard_inspect
|
|
281
313
|
|
|
282
314
|
"#<#{self.class.name} env_name=#{db_config.env_name.inspect}#{name_field} role=#{role.inspect}#{shard_field}>"
|
|
283
315
|
end
|
|
@@ -307,6 +339,14 @@ module ActiveRecord
|
|
|
307
339
|
InternalMetadata.new(self)
|
|
308
340
|
end
|
|
309
341
|
|
|
342
|
+
def activate
|
|
343
|
+
@activated = true
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
def activated?
|
|
347
|
+
@activated
|
|
348
|
+
end
|
|
349
|
+
|
|
310
350
|
# Retrieve the connection associated with the current thread, or call
|
|
311
351
|
# #checkout to obtain one if necessary.
|
|
312
352
|
#
|
|
@@ -314,8 +354,9 @@ module ActiveRecord
|
|
|
314
354
|
# held in a cache keyed by a thread.
|
|
315
355
|
def lease_connection
|
|
316
356
|
lease = connection_lease
|
|
317
|
-
lease.sticky = true
|
|
318
357
|
lease.connection ||= checkout
|
|
358
|
+
lease.sticky = true
|
|
359
|
+
lease.connection
|
|
319
360
|
end
|
|
320
361
|
|
|
321
362
|
def permanent_lease? # :nodoc:
|
|
@@ -333,6 +374,7 @@ module ActiveRecord
|
|
|
333
374
|
end
|
|
334
375
|
|
|
335
376
|
@pinned_connection.lock_thread = ActiveSupport::IsolatedExecutionState.context if lock_thread
|
|
377
|
+
@pinned_connection.pinned = true
|
|
336
378
|
@pinned_connection.verify! # eagerly validate the connection
|
|
337
379
|
@pinned_connection.begin_transaction joinable: false, _lazy: false
|
|
338
380
|
end
|
|
@@ -355,6 +397,7 @@ module ActiveRecord
|
|
|
355
397
|
end
|
|
356
398
|
|
|
357
399
|
if @pinned_connection.nil?
|
|
400
|
+
connection.pinned = false
|
|
358
401
|
connection.steal!
|
|
359
402
|
connection.lock_thread = nil
|
|
360
403
|
checkin(connection)
|
|
@@ -386,6 +429,8 @@ module ActiveRecord
|
|
|
386
429
|
# #lease_connection or #with_connection methods, connections obtained through
|
|
387
430
|
# #checkout will not be automatically released.
|
|
388
431
|
def release_connection(existing_lease = nil)
|
|
432
|
+
return if self.discarded?
|
|
433
|
+
|
|
389
434
|
if conn = connection_lease.release
|
|
390
435
|
checkin conn
|
|
391
436
|
return true
|
|
@@ -423,6 +468,24 @@ module ActiveRecord
|
|
|
423
468
|
end
|
|
424
469
|
end
|
|
425
470
|
|
|
471
|
+
def with_pool_transaction_isolation_level(isolation_level, transaction_open, &block) # :nodoc:
|
|
472
|
+
if !ActiveRecord.default_transaction_isolation_level.nil?
|
|
473
|
+
begin
|
|
474
|
+
if transaction_open && self.pool_transaction_isolation_level != ActiveRecord.default_transaction_isolation_level
|
|
475
|
+
raise ActiveRecord::TransactionIsolationError, "cannot set default isolation level while transaction is open"
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
old_level = self.pool_transaction_isolation_level
|
|
479
|
+
self.pool_transaction_isolation_level = isolation_level
|
|
480
|
+
yield
|
|
481
|
+
ensure
|
|
482
|
+
self.pool_transaction_isolation_level = old_level
|
|
483
|
+
end
|
|
484
|
+
else
|
|
485
|
+
yield
|
|
486
|
+
end
|
|
487
|
+
end
|
|
488
|
+
|
|
426
489
|
# Returns true if a connection has already been opened.
|
|
427
490
|
def connected?
|
|
428
491
|
synchronize { @connections.any?(&:connected?) }
|
|
@@ -450,18 +513,26 @@ module ActiveRecord
|
|
|
450
513
|
# connections in the pool within a timeout interval (default duration is
|
|
451
514
|
# <tt>spec.db_config.checkout_timeout * 2</tt> seconds).
|
|
452
515
|
def disconnect(raise_on_acquisition_timeout = true)
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
516
|
+
@reaper_lock.synchronize do
|
|
517
|
+
return if self.discarded?
|
|
518
|
+
|
|
519
|
+
with_exclusively_acquired_all_connections(raise_on_acquisition_timeout) do
|
|
520
|
+
synchronize do
|
|
521
|
+
return if self.discarded?
|
|
522
|
+
@connections.each do |conn|
|
|
523
|
+
if conn.in_use?
|
|
524
|
+
conn.steal!
|
|
525
|
+
checkin conn
|
|
526
|
+
end
|
|
527
|
+
conn.disconnect!
|
|
459
528
|
end
|
|
460
|
-
|
|
529
|
+
@connections = []
|
|
530
|
+
@leases.clear
|
|
531
|
+
@available.clear
|
|
532
|
+
|
|
533
|
+
# Stop maintaining the minimum size until reactivated
|
|
534
|
+
@activated = false
|
|
461
535
|
end
|
|
462
|
-
@connections = []
|
|
463
|
-
@leases.clear
|
|
464
|
-
@available.clear
|
|
465
536
|
end
|
|
466
537
|
end
|
|
467
538
|
end
|
|
@@ -482,12 +553,14 @@ module ActiveRecord
|
|
|
482
553
|
#
|
|
483
554
|
# See AbstractAdapter#discard!
|
|
484
555
|
def discard! # :nodoc:
|
|
485
|
-
synchronize do
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
conn
|
|
556
|
+
@reaper_lock.synchronize do
|
|
557
|
+
synchronize do
|
|
558
|
+
return if self.discarded?
|
|
559
|
+
@connections.each do |conn|
|
|
560
|
+
conn.discard!
|
|
561
|
+
end
|
|
562
|
+
@connections = @available = @leases = nil
|
|
489
563
|
end
|
|
490
|
-
@connections = @available = @leases = nil
|
|
491
564
|
end
|
|
492
565
|
end
|
|
493
566
|
|
|
@@ -495,7 +568,17 @@ module ActiveRecord
|
|
|
495
568
|
@connections.nil?
|
|
496
569
|
end
|
|
497
570
|
|
|
498
|
-
|
|
571
|
+
def maintainable? # :nodoc:
|
|
572
|
+
synchronize do
|
|
573
|
+
@connections&.size&.> 0 || (activated? && @min_connections > 0)
|
|
574
|
+
end
|
|
575
|
+
end
|
|
576
|
+
|
|
577
|
+
def reaper_lock(&block) # :nodoc:
|
|
578
|
+
@reaper_lock.synchronize(&block)
|
|
579
|
+
end
|
|
580
|
+
|
|
581
|
+
# Clears reloadable connections from the pool and re-connects connections that
|
|
499
582
|
# require reloading.
|
|
500
583
|
#
|
|
501
584
|
# Raises:
|
|
@@ -518,7 +601,7 @@ module ActiveRecord
|
|
|
518
601
|
end
|
|
519
602
|
end
|
|
520
603
|
|
|
521
|
-
# Clears
|
|
604
|
+
# Clears reloadable connections from the pool and re-connects connections that
|
|
522
605
|
# require reloading.
|
|
523
606
|
#
|
|
524
607
|
# The pool first tries to gain ownership of all connections. If unable to
|
|
@@ -578,11 +661,7 @@ module ActiveRecord
|
|
|
578
661
|
conn.lock.synchronize do
|
|
579
662
|
synchronize do
|
|
580
663
|
connection_lease.clear(conn)
|
|
581
|
-
|
|
582
|
-
conn._run_checkin_callbacks do
|
|
583
|
-
conn.expire
|
|
584
|
-
end
|
|
585
|
-
|
|
664
|
+
conn.expire
|
|
586
665
|
@available.add conn
|
|
587
666
|
end
|
|
588
667
|
end
|
|
@@ -600,7 +679,7 @@ module ActiveRecord
|
|
|
600
679
|
@available.delete conn
|
|
601
680
|
|
|
602
681
|
# @available.any_waiting? => true means that prior to removing this
|
|
603
|
-
# conn, the pool was at its max size (@connections.size == @
|
|
682
|
+
# conn, the pool was at its max size (@connections.size == @max_connections).
|
|
604
683
|
# This would mean that any threads stuck waiting in the queue wouldn't
|
|
605
684
|
# know they could checkout_new_connection, so let's do it for them.
|
|
606
685
|
# Because condition-wait loop is encapsulated in the Queue class
|
|
@@ -608,14 +687,14 @@ module ActiveRecord
|
|
|
608
687
|
# that are "stuck" there are helpless. They have no way of creating
|
|
609
688
|
# new connections and are completely reliant on us feeding available
|
|
610
689
|
# connections into the Queue.
|
|
611
|
-
needs_new_connection = @available.
|
|
690
|
+
needs_new_connection = @available.num_waiting > @maintaining
|
|
612
691
|
end
|
|
613
692
|
|
|
614
693
|
# This is intentionally done outside of the synchronized section as we
|
|
615
694
|
# would like not to hold the main mutex while checking out new connections.
|
|
616
695
|
# Thus there is some chance that needs_new_connection information is now
|
|
617
696
|
# stale, we can live with that (bulk_make_new_connections will make
|
|
618
|
-
# sure not to exceed the pool's @
|
|
697
|
+
# sure not to exceed the pool's @max_connections limit).
|
|
619
698
|
bulk_make_new_connections(1) if needs_new_connection
|
|
620
699
|
end
|
|
621
700
|
|
|
@@ -648,11 +727,27 @@ module ActiveRecord
|
|
|
648
727
|
def flush(minimum_idle = @idle_timeout)
|
|
649
728
|
return if minimum_idle.nil?
|
|
650
729
|
|
|
651
|
-
|
|
730
|
+
removed_connections = synchronize do
|
|
652
731
|
return if self.discarded?
|
|
653
|
-
|
|
732
|
+
|
|
733
|
+
idle_connections = @connections.select do |conn|
|
|
654
734
|
!conn.in_use? && conn.seconds_idle >= minimum_idle
|
|
655
|
-
end.
|
|
735
|
+
end.sort_by { |conn| -conn.seconds_idle } # sort longest idle first
|
|
736
|
+
|
|
737
|
+
# Don't go below our configured pool minimum unless we're flushing
|
|
738
|
+
# everything
|
|
739
|
+
idles_to_retain =
|
|
740
|
+
if minimum_idle > 0
|
|
741
|
+
@min_connections - (@connections.size - idle_connections.size)
|
|
742
|
+
else
|
|
743
|
+
0
|
|
744
|
+
end
|
|
745
|
+
|
|
746
|
+
if idles_to_retain > 0
|
|
747
|
+
idle_connections.pop idles_to_retain
|
|
748
|
+
end
|
|
749
|
+
|
|
750
|
+
idle_connections.each do |conn|
|
|
656
751
|
conn.lease
|
|
657
752
|
|
|
658
753
|
@available.delete conn
|
|
@@ -660,22 +755,109 @@ module ActiveRecord
|
|
|
660
755
|
end
|
|
661
756
|
end
|
|
662
757
|
|
|
663
|
-
|
|
758
|
+
removed_connections.each do |conn|
|
|
664
759
|
conn.disconnect!
|
|
665
760
|
end
|
|
666
761
|
end
|
|
667
762
|
|
|
668
763
|
# Disconnect all currently idle connections. Connections currently checked
|
|
669
|
-
# out are unaffected.
|
|
764
|
+
# out are unaffected. The pool will stop maintaining its minimum size until
|
|
765
|
+
# it is reactivated (such as by a subsequent checkout).
|
|
670
766
|
def flush!
|
|
671
767
|
reap
|
|
672
768
|
flush(-1)
|
|
769
|
+
|
|
770
|
+
# Stop maintaining the minimum size until reactivated
|
|
771
|
+
@activated = false
|
|
772
|
+
end
|
|
773
|
+
|
|
774
|
+
# Ensure that the pool contains at least the configured minimum number of
|
|
775
|
+
# connections.
|
|
776
|
+
def prepopulate
|
|
777
|
+
need_new_connections = nil
|
|
778
|
+
|
|
779
|
+
synchronize do
|
|
780
|
+
return if self.discarded?
|
|
781
|
+
|
|
782
|
+
# We don't want to start prepopulating until we know the pool is wanted,
|
|
783
|
+
# so we can avoid maintaining full pools in one-off scripts etc.
|
|
784
|
+
return unless @activated
|
|
785
|
+
|
|
786
|
+
need_new_connections = @connections.size < @min_connections
|
|
787
|
+
end
|
|
788
|
+
|
|
789
|
+
if need_new_connections
|
|
790
|
+
while new_conn = try_to_checkout_new_connection { @connections.size < @min_connections }
|
|
791
|
+
new_conn.allow_preconnect = true
|
|
792
|
+
checkin(new_conn)
|
|
793
|
+
end
|
|
794
|
+
end
|
|
795
|
+
end
|
|
796
|
+
|
|
797
|
+
def retire_old_connections(max_age = @max_age)
|
|
798
|
+
max_age ||= Float::INFINITY
|
|
799
|
+
|
|
800
|
+
sequential_maintenance -> c { c.connection_age&.>= c.pool_jitter(max_age) } do |conn|
|
|
801
|
+
# Disconnect, then return the adapter to the pool. Preconnect will
|
|
802
|
+
# handle the rest.
|
|
803
|
+
conn.disconnect!
|
|
804
|
+
end
|
|
805
|
+
end
|
|
806
|
+
|
|
807
|
+
# Preconnect all connections in the pool. This saves pool users from
|
|
808
|
+
# having to wait for a connection to be established when first using it
|
|
809
|
+
# after checkout.
|
|
810
|
+
def preconnect
|
|
811
|
+
sequential_maintenance -> c { (!c.connected? || !c.verified?) && c.allow_preconnect } do |conn|
|
|
812
|
+
conn.connect!
|
|
813
|
+
rescue
|
|
814
|
+
# Wholesale rescue: there's nothing we can do but move on. The
|
|
815
|
+
# connection will go back to the pool, and the next consumer will
|
|
816
|
+
# presumably try to connect again -- which will either work, or
|
|
817
|
+
# fail and they'll be able to report the exception.
|
|
818
|
+
end
|
|
819
|
+
end
|
|
820
|
+
|
|
821
|
+
# Prod any connections that have been idle for longer than the configured
|
|
822
|
+
# keepalive time. This will incidentally verify the connection is still
|
|
823
|
+
# alive, but the main purpose is to show the server (and any intermediate
|
|
824
|
+
# network hops) that we're still here and using the connection.
|
|
825
|
+
def keep_alive(threshold = @keepalive)
|
|
826
|
+
return if threshold.nil?
|
|
827
|
+
|
|
828
|
+
sequential_maintenance -> c { (c.seconds_since_last_activity || 0) > c.pool_jitter(threshold) } do |conn|
|
|
829
|
+
# conn.active? will cause some amount of network activity, which is all
|
|
830
|
+
# we need to provide a keepalive signal.
|
|
831
|
+
#
|
|
832
|
+
# If it returns false, the connection is already broken; disconnect,
|
|
833
|
+
# so it can be found and repaired.
|
|
834
|
+
conn.disconnect! unless conn.active?
|
|
835
|
+
end
|
|
836
|
+
end
|
|
837
|
+
|
|
838
|
+
# Immediately mark all current connections as due for replacement,
|
|
839
|
+
# equivalent to them having reached +max_age+ -- even if there is
|
|
840
|
+
# no +max_age+ configured.
|
|
841
|
+
def recycle!
|
|
842
|
+
synchronize do
|
|
843
|
+
return if self.discarded?
|
|
844
|
+
|
|
845
|
+
@connections.each do |conn|
|
|
846
|
+
conn.force_retirement
|
|
847
|
+
end
|
|
848
|
+
end
|
|
849
|
+
|
|
850
|
+
retire_old_connections
|
|
673
851
|
end
|
|
674
852
|
|
|
675
853
|
def num_waiting_in_queue # :nodoc:
|
|
676
854
|
@available.num_waiting
|
|
677
855
|
end
|
|
678
856
|
|
|
857
|
+
def num_available_in_queue # :nodoc:
|
|
858
|
+
@available.size
|
|
859
|
+
end
|
|
860
|
+
|
|
679
861
|
# Returns the connection pool's usage statistic.
|
|
680
862
|
#
|
|
681
863
|
# ActiveRecord::Base.connection_pool.stat # => { size: 15, connections: 1, busy: 1, dead: 0, idle: 0, waiting: 0, checkout_timeout: 5 }
|
|
@@ -706,6 +888,16 @@ module ActiveRecord
|
|
|
706
888
|
raise ex.set_pool(self)
|
|
707
889
|
end
|
|
708
890
|
|
|
891
|
+
def pool_transaction_isolation_level
|
|
892
|
+
isolation_level_key = "activerecord_pool_transaction_isolation_level_#{db_config.name}"
|
|
893
|
+
ActiveSupport::IsolatedExecutionState[isolation_level_key]
|
|
894
|
+
end
|
|
895
|
+
|
|
896
|
+
def pool_transaction_isolation_level=(isolation_level)
|
|
897
|
+
isolation_level_key = "activerecord_pool_transaction_isolation_level_#{db_config.name}"
|
|
898
|
+
ActiveSupport::IsolatedExecutionState[isolation_level_key] = isolation_level
|
|
899
|
+
end
|
|
900
|
+
|
|
709
901
|
private
|
|
710
902
|
def connection_lease
|
|
711
903
|
@leases[ActiveSupport::IsolatedExecutionState.context]
|
|
@@ -715,7 +907,9 @@ module ActiveRecord
|
|
|
715
907
|
case ActiveRecord.async_query_executor
|
|
716
908
|
when :multi_thread_pool
|
|
717
909
|
if @db_config.max_threads > 0
|
|
910
|
+
name_with_shard = [name_inspect, shard_inspect].join("-").tr("_", "-")
|
|
718
911
|
Concurrent::ThreadPoolExecutor.new(
|
|
912
|
+
name: "ActiveRecord-#{name_with_shard}-async-query-executor",
|
|
719
913
|
min_threads: @db_config.min_threads,
|
|
720
914
|
max_threads: @db_config.max_threads,
|
|
721
915
|
max_queue: @db_config.max_queue,
|
|
@@ -727,11 +921,109 @@ module ActiveRecord
|
|
|
727
921
|
end
|
|
728
922
|
end
|
|
729
923
|
|
|
924
|
+
# Perform maintenance work on pool connections. This method will
|
|
925
|
+
# select a connection to work on by calling the +candidate_selector+
|
|
926
|
+
# proc while holding the pool lock. If a connection is selected, it
|
|
927
|
+
# will be checked out for maintenance and passed to the
|
|
928
|
+
# +maintenance_work+ proc. The connection will always be returned to
|
|
929
|
+
# the pool after the proc completes.
|
|
930
|
+
#
|
|
931
|
+
# If the pool has async threads, all work will be scheduled there.
|
|
932
|
+
# Otherwise, this method will block until all work is complete.
|
|
933
|
+
#
|
|
934
|
+
# Each connection will only be processed once per call to this method,
|
|
935
|
+
# but (particularly in the async case) there is no protection against
|
|
936
|
+
# a second call to this method starting to work through the list
|
|
937
|
+
# before the first call has completed. (Though regular pool behavior
|
|
938
|
+
# will prevent two instances from working on the same specific
|
|
939
|
+
# connection at the same time.)
|
|
940
|
+
def sequential_maintenance(candidate_selector, &maintenance_work)
|
|
941
|
+
# This hash doesn't need to be synchronized, because it's only
|
|
942
|
+
# used by one thread at a time: the +perform_work+ block gives
|
|
943
|
+
# up its right to +connections_visited+ when it schedules the
|
|
944
|
+
# next iteration.
|
|
945
|
+
connections_visited = Hash.new(false)
|
|
946
|
+
connections_visited.compare_by_identity
|
|
947
|
+
|
|
948
|
+
perform_work = lambda do
|
|
949
|
+
connection_to_maintain = nil
|
|
950
|
+
|
|
951
|
+
synchronize do
|
|
952
|
+
unless self.discarded?
|
|
953
|
+
if connection_to_maintain = @connections.select { |conn| !conn.in_use? }.select(&candidate_selector).sort_by(&:seconds_idle).find { |conn| !connections_visited[conn] }
|
|
954
|
+
checkout_for_maintenance connection_to_maintain
|
|
955
|
+
end
|
|
956
|
+
end
|
|
957
|
+
end
|
|
958
|
+
|
|
959
|
+
if connection_to_maintain
|
|
960
|
+
connections_visited[connection_to_maintain] = true
|
|
961
|
+
|
|
962
|
+
# If we're running async, we can schedule the next round of work
|
|
963
|
+
# as soon as we've grabbed a connection to work on.
|
|
964
|
+
@async_executor&.post(&perform_work)
|
|
965
|
+
|
|
966
|
+
begin
|
|
967
|
+
maintenance_work.call connection_to_maintain
|
|
968
|
+
ensure
|
|
969
|
+
return_from_maintenance connection_to_maintain
|
|
970
|
+
end
|
|
971
|
+
|
|
972
|
+
true
|
|
973
|
+
end
|
|
974
|
+
end
|
|
975
|
+
|
|
976
|
+
if @async_executor
|
|
977
|
+
@async_executor.post(&perform_work)
|
|
978
|
+
else
|
|
979
|
+
nil while perform_work.call
|
|
980
|
+
end
|
|
981
|
+
end
|
|
982
|
+
|
|
983
|
+
# Directly check a specific connection out of the pool. Skips callbacks.
|
|
984
|
+
#
|
|
985
|
+
# The connection must later either #return_from_maintenance or
|
|
986
|
+
# #remove_from_maintenance, or the pool will hang.
|
|
987
|
+
def checkout_for_maintenance(conn)
|
|
988
|
+
synchronize do
|
|
989
|
+
@maintaining += 1
|
|
990
|
+
@available.delete(conn)
|
|
991
|
+
conn.lease
|
|
992
|
+
conn
|
|
993
|
+
end
|
|
994
|
+
end
|
|
995
|
+
|
|
996
|
+
# Return a connection to the pool after it has been checked out for
|
|
997
|
+
# maintenance. Does not update the connection's idle time, and skips
|
|
998
|
+
# callbacks.
|
|
999
|
+
#--
|
|
1000
|
+
# We assume that a connection that has required maintenance is less
|
|
1001
|
+
# desirable (either it's been idle for a long time, or it was just
|
|
1002
|
+
# created and hasn't been used yet). We'll put it at the back of the
|
|
1003
|
+
# queue.
|
|
1004
|
+
def return_from_maintenance(conn)
|
|
1005
|
+
synchronize do
|
|
1006
|
+
conn.expire(false)
|
|
1007
|
+
@available.add_back(conn)
|
|
1008
|
+
@maintaining -= 1
|
|
1009
|
+
end
|
|
1010
|
+
end
|
|
1011
|
+
|
|
1012
|
+
# Remove a connection from the pool after it has been checked out for
|
|
1013
|
+
# maintenance. It will be automatically replaced with a new connection if
|
|
1014
|
+
# necessary.
|
|
1015
|
+
def remove_from_maintenance(conn)
|
|
1016
|
+
synchronize do
|
|
1017
|
+
@maintaining -= 1
|
|
1018
|
+
remove conn
|
|
1019
|
+
end
|
|
1020
|
+
end
|
|
1021
|
+
|
|
730
1022
|
#--
|
|
731
1023
|
# this is unfortunately not concurrent
|
|
732
1024
|
def bulk_make_new_connections(num_new_conns_needed)
|
|
733
1025
|
num_new_conns_needed.times do
|
|
734
|
-
# try_to_checkout_new_connection will not exceed pool's @
|
|
1026
|
+
# try_to_checkout_new_connection will not exceed pool's @max_connections limit
|
|
735
1027
|
if new_conn = try_to_checkout_new_connection
|
|
736
1028
|
# make the new_conn available to the starving threads stuck @available Queue
|
|
737
1029
|
checkin(new_conn)
|
|
@@ -744,9 +1036,11 @@ module ActiveRecord
|
|
|
744
1036
|
# wrap it in +synchronize+ because some pool's actions are allowed
|
|
745
1037
|
# to be performed outside of the main +synchronize+ block.
|
|
746
1038
|
def with_exclusively_acquired_all_connections(raise_on_acquisition_timeout = true)
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
1039
|
+
@reaper_lock.synchronize do
|
|
1040
|
+
with_new_connections_blocked do
|
|
1041
|
+
attempt_to_checkout_all_existing_connections(raise_on_acquisition_timeout)
|
|
1042
|
+
yield
|
|
1043
|
+
end
|
|
750
1044
|
end
|
|
751
1045
|
end
|
|
752
1046
|
|
|
@@ -866,13 +1160,13 @@ module ActiveRecord
|
|
|
866
1160
|
# <tt>synchronize { conn.lease }</tt> in this method, but by leaving it to <tt>@available.poll</tt>
|
|
867
1161
|
# and +try_to_checkout_new_connection+ we can piggyback on +synchronize+ sections
|
|
868
1162
|
# of the said methods and avoid an additional +synchronize+ overhead.
|
|
869
|
-
if conn = @available.poll || try_to_checkout_new_connection
|
|
1163
|
+
if conn = @available.poll || try_to_queue_for_background_connection(checkout_timeout) || try_to_checkout_new_connection
|
|
870
1164
|
conn
|
|
871
1165
|
else
|
|
872
1166
|
reap
|
|
873
1167
|
# Retry after reaping, which may return an available connection,
|
|
874
1168
|
# remove an inactive connection, or both
|
|
875
|
-
if conn = @available.poll || try_to_checkout_new_connection
|
|
1169
|
+
if conn = @available.poll || try_to_queue_for_background_connection(checkout_timeout) || try_to_checkout_new_connection
|
|
876
1170
|
conn
|
|
877
1171
|
else
|
|
878
1172
|
@available.poll(checkout_timeout)
|
|
@@ -882,6 +1176,31 @@ module ActiveRecord
|
|
|
882
1176
|
raise ex.set_pool(self)
|
|
883
1177
|
end
|
|
884
1178
|
|
|
1179
|
+
#--
|
|
1180
|
+
# If new connections are already being established in the background,
|
|
1181
|
+
# and there are fewer threads already waiting than the number of
|
|
1182
|
+
# upcoming connections, we can just get in queue and wait to be handed a
|
|
1183
|
+
# connection. This avoids us overshooting the required connection count
|
|
1184
|
+
# by starting a new connection ourselves, and is likely to be faster
|
|
1185
|
+
# too (because at least some of the time it takes to establish a new
|
|
1186
|
+
# connection must have already passed).
|
|
1187
|
+
#
|
|
1188
|
+
# If background connections are available, this method will block and
|
|
1189
|
+
# return a connection. If no background connections are available, it
|
|
1190
|
+
# will immediately return +nil+.
|
|
1191
|
+
def try_to_queue_for_background_connection(checkout_timeout)
|
|
1192
|
+
return unless @maintaining > 0
|
|
1193
|
+
|
|
1194
|
+
synchronize do
|
|
1195
|
+
return unless @maintaining > @available.num_waiting
|
|
1196
|
+
|
|
1197
|
+
# We are guaranteed the "maintaining" thread will return its promised
|
|
1198
|
+
# connection within one maintenance-unit of time. Thus we can safely
|
|
1199
|
+
# do a blocking wait with (functionally) no timeout.
|
|
1200
|
+
@available.poll(100)
|
|
1201
|
+
end
|
|
1202
|
+
end
|
|
1203
|
+
|
|
885
1204
|
#--
|
|
886
1205
|
# if owner_thread param is omitted, this must be called in synchronize block
|
|
887
1206
|
def remove_connection_from_thread_cache(conn, owner_thread = conn.owner)
|
|
@@ -891,17 +1210,26 @@ module ActiveRecord
|
|
|
891
1210
|
end
|
|
892
1211
|
alias_method :release, :remove_connection_from_thread_cache
|
|
893
1212
|
|
|
894
|
-
# If the pool is not at a <tt>@
|
|
1213
|
+
# If the pool is not at a <tt>@max_connections</tt> limit, establish new connection. Connecting
|
|
895
1214
|
# to the DB is done outside main synchronized section.
|
|
1215
|
+
#
|
|
1216
|
+
# If a block is supplied, it is an additional constraint (checked while holding the
|
|
1217
|
+
# pool lock) on whether a new connection should be established.
|
|
896
1218
|
#--
|
|
897
1219
|
# Implementation constraint: a newly established connection returned by this
|
|
898
1220
|
# method must be in the +.leased+ state.
|
|
899
1221
|
def try_to_checkout_new_connection
|
|
900
1222
|
# first in synchronized section check if establishing new conns is allowed
|
|
901
|
-
# and increment @now_connecting, to prevent overstepping this pool's @
|
|
1223
|
+
# and increment @now_connecting, to prevent overstepping this pool's @max_connections
|
|
902
1224
|
# constraint
|
|
903
1225
|
do_checkout = synchronize do
|
|
904
|
-
if
|
|
1226
|
+
return if self.discarded?
|
|
1227
|
+
|
|
1228
|
+
if @threads_blocking_new_connections.zero? && (@max_connections.nil? || (@connections.size + @now_connecting) < @max_connections) && (!block_given? || yield)
|
|
1229
|
+
if @connections.size > 0 || @original_context != ActiveSupport::IsolatedExecutionState.context
|
|
1230
|
+
@activated = true
|
|
1231
|
+
end
|
|
1232
|
+
|
|
905
1233
|
@now_connecting += 1
|
|
906
1234
|
end
|
|
907
1235
|
end
|
|
@@ -912,12 +1240,16 @@ module ActiveRecord
|
|
|
912
1240
|
conn = checkout_new_connection
|
|
913
1241
|
ensure
|
|
914
1242
|
synchronize do
|
|
1243
|
+
@now_connecting -= 1
|
|
915
1244
|
if conn
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
1245
|
+
if self.discarded?
|
|
1246
|
+
conn.discard!
|
|
1247
|
+
else
|
|
1248
|
+
adopt_connection(conn)
|
|
1249
|
+
# returned conn needs to be already leased
|
|
1250
|
+
conn.lease
|
|
1251
|
+
end
|
|
919
1252
|
end
|
|
920
|
-
@now_connecting -= 1
|
|
921
1253
|
end
|
|
922
1254
|
end
|
|
923
1255
|
end
|
|
@@ -940,15 +1272,20 @@ module ActiveRecord
|
|
|
940
1272
|
end
|
|
941
1273
|
|
|
942
1274
|
def checkout_and_verify(c)
|
|
943
|
-
c.
|
|
944
|
-
c.clean!
|
|
945
|
-
end
|
|
946
|
-
c
|
|
1275
|
+
c.clean!
|
|
947
1276
|
rescue Exception
|
|
948
1277
|
remove c
|
|
949
1278
|
c.disconnect!
|
|
950
1279
|
raise
|
|
951
1280
|
end
|
|
1281
|
+
|
|
1282
|
+
def name_inspect
|
|
1283
|
+
db_config.name.inspect unless db_config.name == "primary"
|
|
1284
|
+
end
|
|
1285
|
+
|
|
1286
|
+
def shard_inspect
|
|
1287
|
+
shard.inspect unless shard == :default
|
|
1288
|
+
end
|
|
952
1289
|
end
|
|
953
1290
|
end
|
|
954
1291
|
end
|