activerecord 5.0.7 → 5.1.7
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/CHANGELOG.md +657 -2080
- data/MIT-LICENSE +1 -1
- data/README.rdoc +1 -1
- data/examples/performance.rb +28 -28
- data/examples/simple.rb +3 -3
- data/lib/active_record/aggregations.rb +244 -244
- data/lib/active_record/association_relation.rb +5 -5
- data/lib/active_record/associations/alias_tracker.rb +10 -11
- data/lib/active_record/associations/association.rb +23 -5
- data/lib/active_record/associations/association_scope.rb +95 -81
- data/lib/active_record/associations/belongs_to_association.rb +7 -4
- data/lib/active_record/associations/builder/belongs_to.rb +30 -16
- data/lib/active_record/associations/builder/collection_association.rb +1 -2
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +27 -27
- data/lib/active_record/associations/collection_association.rb +36 -205
- data/lib/active_record/associations/collection_proxy.rb +132 -63
- data/lib/active_record/associations/has_many_association.rb +10 -19
- data/lib/active_record/associations/has_many_through_association.rb +12 -4
- data/lib/active_record/associations/has_one_association.rb +24 -28
- data/lib/active_record/associations/has_one_through_association.rb +5 -1
- data/lib/active_record/associations/join_dependency/join_association.rb +4 -28
- data/lib/active_record/associations/join_dependency/join_base.rb +1 -1
- data/lib/active_record/associations/join_dependency/join_part.rb +1 -1
- data/lib/active_record/associations/join_dependency.rb +121 -118
- data/lib/active_record/associations/preloader/association.rb +64 -64
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -2
- data/lib/active_record/associations/preloader/collection_association.rb +6 -6
- data/lib/active_record/associations/preloader/has_many.rb +0 -2
- data/lib/active_record/associations/preloader/singular_association.rb +6 -8
- data/lib/active_record/associations/preloader/through_association.rb +41 -41
- data/lib/active_record/associations/preloader.rb +94 -94
- data/lib/active_record/associations/singular_association.rb +8 -25
- data/lib/active_record/associations/through_association.rb +2 -5
- data/lib/active_record/associations.rb +1591 -1562
- data/lib/active_record/attribute/user_provided_default.rb +4 -2
- data/lib/active_record/attribute.rb +98 -71
- data/lib/active_record/attribute_assignment.rb +61 -61
- data/lib/active_record/attribute_decorators.rb +35 -13
- data/lib/active_record/attribute_methods/before_type_cast.rb +7 -7
- data/lib/active_record/attribute_methods/dirty.rb +229 -46
- data/lib/active_record/attribute_methods/primary_key.rb +74 -73
- data/lib/active_record/attribute_methods/read.rb +39 -35
- data/lib/active_record/attribute_methods/serialization.rb +7 -7
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +35 -58
- data/lib/active_record/attribute_methods/write.rb +30 -33
- data/lib/active_record/attribute_methods.rb +56 -65
- data/lib/active_record/attribute_mutation_tracker.rb +63 -11
- data/lib/active_record/attribute_set/builder.rb +27 -33
- data/lib/active_record/attribute_set/yaml_encoder.rb +41 -0
- data/lib/active_record/attribute_set.rb +9 -6
- data/lib/active_record/attributes.rb +22 -22
- data/lib/active_record/autosave_association.rb +18 -13
- data/lib/active_record/base.rb +24 -22
- data/lib/active_record/callbacks.rb +56 -14
- data/lib/active_record/coders/yaml_column.rb +9 -11
- data/lib/active_record/collection_cache_key.rb +3 -4
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +330 -284
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +1 -3
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +39 -37
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +32 -27
- data/lib/active_record/connection_adapters/abstract/quoting.rb +62 -51
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +10 -20
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +74 -79
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +53 -41
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +120 -100
- data/lib/active_record/connection_adapters/abstract/transaction.rb +49 -43
- data/lib/active_record/connection_adapters/abstract_adapter.rb +165 -135
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +404 -424
- data/lib/active_record/connection_adapters/column.rb +26 -4
- data/lib/active_record/connection_adapters/connection_specification.rb +128 -118
- data/lib/active_record/connection_adapters/mysql/column.rb +6 -31
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +36 -49
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +22 -22
- data/lib/active_record/connection_adapters/mysql/quoting.rb +6 -12
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +49 -45
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +16 -19
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +54 -28
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +43 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +7 -6
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +23 -27
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +32 -53
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +3 -3
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +19 -9
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +3 -3
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +16 -16
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +0 -10
- data/lib/active_record/connection_adapters/postgresql/oid/{rails_5_1_point.rb → legacy_point.rb} +9 -16
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +13 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +28 -8
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +32 -30
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -1
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +51 -51
- data/lib/active_record/connection_adapters/postgresql/oid.rb +22 -21
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +40 -35
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +37 -24
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +19 -23
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +182 -222
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +6 -4
- data/lib/active_record/connection_adapters/postgresql/utils.rb +7 -5
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +198 -167
- data/lib/active_record/connection_adapters/schema_cache.rb +16 -7
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +3 -3
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +1 -1
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +16 -19
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +1 -8
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +28 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +17 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +32 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +184 -167
- data/lib/active_record/connection_adapters/statement_pool.rb +7 -7
- data/lib/active_record/connection_handling.rb +14 -26
- data/lib/active_record/core.rb +109 -93
- data/lib/active_record/counter_cache.rb +60 -13
- data/lib/active_record/define_callbacks.rb +20 -0
- data/lib/active_record/dynamic_matchers.rb +80 -79
- data/lib/active_record/enum.rb +8 -6
- data/lib/active_record/errors.rb +64 -15
- data/lib/active_record/explain.rb +1 -2
- data/lib/active_record/explain_registry.rb +1 -1
- data/lib/active_record/explain_subscriber.rb +7 -4
- data/lib/active_record/fixture_set/file.rb +11 -8
- data/lib/active_record/fixtures.rb +66 -53
- data/lib/active_record/gem_version.rb +1 -1
- data/lib/active_record/inheritance.rb +93 -79
- data/lib/active_record/integration.rb +7 -7
- data/lib/active_record/internal_metadata.rb +3 -16
- data/lib/active_record/legacy_yaml_adapter.rb +1 -1
- data/lib/active_record/locking/optimistic.rb +69 -74
- data/lib/active_record/locking/pessimistic.rb +10 -1
- data/lib/active_record/log_subscriber.rb +23 -28
- data/lib/active_record/migration/command_recorder.rb +94 -94
- data/lib/active_record/migration/compatibility.rb +100 -47
- data/lib/active_record/migration/join_table.rb +6 -6
- data/lib/active_record/migration.rb +153 -155
- data/lib/active_record/model_schema.rb +94 -107
- data/lib/active_record/nested_attributes.rb +200 -199
- data/lib/active_record/null_relation.rb +11 -34
- data/lib/active_record/persistence.rb +65 -50
- data/lib/active_record/query_cache.rb +2 -6
- data/lib/active_record/querying.rb +3 -4
- data/lib/active_record/railtie.rb +16 -17
- data/lib/active_record/railties/controller_runtime.rb +6 -2
- data/lib/active_record/railties/databases.rake +105 -133
- data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
- data/lib/active_record/readonly_attributes.rb +2 -2
- data/lib/active_record/reflection.rb +154 -108
- data/lib/active_record/relation/batches/batch_enumerator.rb +1 -1
- data/lib/active_record/relation/batches.rb +80 -51
- data/lib/active_record/relation/calculations.rb +169 -162
- data/lib/active_record/relation/delegation.rb +32 -31
- data/lib/active_record/relation/finder_methods.rb +197 -231
- data/lib/active_record/relation/merger.rb +58 -62
- data/lib/active_record/relation/predicate_builder/array_handler.rb +7 -5
- data/lib/active_record/relation/predicate_builder/association_query_handler.rb +23 -23
- data/lib/active_record/relation/predicate_builder/base_handler.rb +3 -1
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +0 -8
- data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +12 -10
- data/lib/active_record/relation/predicate_builder/range_handler.rb +0 -8
- data/lib/active_record/relation/predicate_builder.rb +92 -89
- data/lib/active_record/relation/query_attribute.rb +1 -1
- data/lib/active_record/relation/query_methods.rb +255 -293
- data/lib/active_record/relation/record_fetch_warning.rb +3 -3
- data/lib/active_record/relation/spawn_methods.rb +4 -5
- data/lib/active_record/relation/where_clause.rb +80 -65
- data/lib/active_record/relation/where_clause_factory.rb +47 -8
- data/lib/active_record/relation.rb +93 -119
- data/lib/active_record/result.rb +41 -32
- data/lib/active_record/runtime_registry.rb +3 -3
- data/lib/active_record/sanitization.rb +176 -192
- data/lib/active_record/schema.rb +3 -3
- data/lib/active_record/schema_dumper.rb +15 -38
- data/lib/active_record/schema_migration.rb +8 -4
- data/lib/active_record/scoping/default.rb +90 -90
- data/lib/active_record/scoping/named.rb +11 -11
- data/lib/active_record/scoping.rb +6 -6
- data/lib/active_record/secure_token.rb +2 -2
- data/lib/active_record/statement_cache.rb +13 -15
- data/lib/active_record/store.rb +31 -32
- data/lib/active_record/suppressor.rb +2 -1
- data/lib/active_record/table_metadata.rb +9 -5
- data/lib/active_record/tasks/database_tasks.rb +65 -55
- data/lib/active_record/tasks/mysql_database_tasks.rb +76 -73
- data/lib/active_record/tasks/postgresql_database_tasks.rb +72 -47
- data/lib/active_record/tasks/sqlite_database_tasks.rb +18 -16
- data/lib/active_record/timestamp.rb +46 -25
- data/lib/active_record/touch_later.rb +1 -2
- data/lib/active_record/transactions.rb +97 -109
- data/lib/active_record/type/adapter_specific_registry.rb +46 -42
- data/lib/active_record/type/decimal_without_scale.rb +13 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +3 -3
- data/lib/active_record/type/internal/abstract_json.rb +4 -0
- data/lib/active_record/type/serialized.rb +14 -8
- data/lib/active_record/type/text.rb +9 -0
- data/lib/active_record/type/time.rb +0 -1
- data/lib/active_record/type/type_map.rb +11 -15
- data/lib/active_record/type/unsigned_integer.rb +15 -0
- data/lib/active_record/type.rb +17 -13
- data/lib/active_record/type_caster/connection.rb +8 -6
- data/lib/active_record/type_caster/map.rb +3 -1
- data/lib/active_record/type_caster.rb +2 -2
- data/lib/active_record/validations/associated.rb +1 -1
- data/lib/active_record/validations/presence.rb +2 -2
- data/lib/active_record/validations/uniqueness.rb +8 -39
- data/lib/active_record/validations.rb +4 -4
- data/lib/active_record/version.rb +1 -1
- data/lib/active_record.rb +20 -20
- data/lib/rails/generators/active_record/migration/migration_generator.rb +37 -34
- data/lib/rails/generators/active_record/migration.rb +1 -1
- data/lib/rails/generators/active_record/model/model_generator.rb +9 -9
- data/lib/rails/generators/active_record.rb +4 -4
- metadata +24 -13
- data/lib/active_record/relation/predicate_builder/class_handler.rb +0 -27
@@ -1,6 +1,6 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "thread"
|
2
|
+
require "concurrent/map"
|
3
|
+
require "monitor"
|
4
4
|
|
5
5
|
module ActiveRecord
|
6
6
|
# Raised when a connection could not be obtained within the connection
|
@@ -69,12 +69,12 @@ module ActiveRecord
|
|
69
69
|
# threads, which can occur if a programmer forgets to close a
|
70
70
|
# connection at the end of a thread or a thread dies unexpectedly.
|
71
71
|
# Regardless of this setting, the Reaper will be invoked before every
|
72
|
-
# blocking wait. (Default nil
|
72
|
+
# blocking wait. (Default +nil+, which means don't schedule the Reaper).
|
73
73
|
#
|
74
74
|
#--
|
75
75
|
# Synchronization policy:
|
76
76
|
# * all public methods can be called outside +synchronize+
|
77
|
-
# * access to these
|
77
|
+
# * access to these instance variables needs to be in +synchronize+:
|
78
78
|
# * @connections
|
79
79
|
# * @now_connecting
|
80
80
|
# * private methods that require being called in a +synchronize+ blocks
|
@@ -116,7 +116,7 @@ module ActiveRecord
|
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
119
|
-
# If +element+ is in the queue, remove and return it, or nil
|
119
|
+
# If +element+ is in the queue, remove and return it, or +nil+.
|
120
120
|
def delete(element)
|
121
121
|
synchronize do
|
122
122
|
@queue.delete(element)
|
@@ -135,7 +135,7 @@ module ActiveRecord
|
|
135
135
|
# If +timeout+ is not given, remove and return the head the
|
136
136
|
# queue if the number of available elements is strictly
|
137
137
|
# greater than the number of threads currently waiting (that
|
138
|
-
# is, don't jump ahead in line). Otherwise, return nil
|
138
|
+
# is, don't jump ahead in line). Otherwise, return +nil+.
|
139
139
|
#
|
140
140
|
# If +timeout+ is given, block if there is no element
|
141
141
|
# available, waiting up to +timeout+ seconds for an element to
|
@@ -150,61 +150,63 @@ module ActiveRecord
|
|
150
150
|
|
151
151
|
private
|
152
152
|
|
153
|
-
|
154
|
-
|
155
|
-
|
153
|
+
def internal_poll(timeout)
|
154
|
+
no_wait_poll || (timeout && wait_poll(timeout))
|
155
|
+
end
|
156
156
|
|
157
|
-
|
158
|
-
|
159
|
-
|
157
|
+
def synchronize(&block)
|
158
|
+
@lock.synchronize(&block)
|
159
|
+
end
|
160
160
|
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
161
|
+
# Test if the queue currently contains any elements.
|
162
|
+
def any?
|
163
|
+
!@queue.empty?
|
164
|
+
end
|
165
165
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
166
|
+
# A thread can remove an element from the queue without
|
167
|
+
# waiting if and only if the number of currently available
|
168
|
+
# connections is strictly greater than the number of waiting
|
169
|
+
# threads.
|
170
|
+
def can_remove_no_wait?
|
171
|
+
@queue.size > @num_waiting
|
172
|
+
end
|
173
173
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
174
|
+
# Removes and returns the head of the queue if possible, or +nil+.
|
175
|
+
def remove
|
176
|
+
@queue.shift
|
177
|
+
end
|
178
178
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
179
|
+
# Remove and return the head the queue if the number of
|
180
|
+
# available elements is strictly greater than the number of
|
181
|
+
# threads currently waiting. Otherwise, return +nil+.
|
182
|
+
def no_wait_poll
|
183
|
+
remove if can_remove_no_wait?
|
184
|
+
end
|
185
185
|
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
186
|
+
# Waits on the queue up to +timeout+ seconds, then removes and
|
187
|
+
# returns the head of the queue.
|
188
|
+
def wait_poll(timeout)
|
189
|
+
@num_waiting += 1
|
190
190
|
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
191
|
+
t0 = Time.now
|
192
|
+
elapsed = 0
|
193
|
+
loop do
|
194
|
+
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
|
195
|
+
@cond.wait(timeout - elapsed)
|
196
|
+
end
|
195
197
|
|
196
|
-
|
198
|
+
return remove if any?
|
197
199
|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
200
|
+
elapsed = Time.now - t0
|
201
|
+
if elapsed >= timeout
|
202
|
+
msg = "could not obtain a connection from the pool within %0.3f seconds (waited %0.3f seconds); all pooled connections were in use" %
|
203
|
+
[timeout, elapsed]
|
204
|
+
raise ConnectionTimeoutError, msg
|
205
|
+
end
|
203
206
|
end
|
207
|
+
ensure
|
208
|
+
@num_waiting -= 1
|
204
209
|
end
|
205
|
-
ensure
|
206
|
-
@num_waiting -= 1
|
207
|
-
end
|
208
210
|
end
|
209
211
|
|
210
212
|
# Adds the ability to turn a basic fair FIFO queue into one
|
@@ -274,15 +276,15 @@ module ActiveRecord
|
|
274
276
|
include BiasableQueue
|
275
277
|
|
276
278
|
private
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
279
|
+
def internal_poll(timeout)
|
280
|
+
conn = super
|
281
|
+
conn.lease if conn
|
282
|
+
conn
|
283
|
+
end
|
282
284
|
end
|
283
285
|
|
284
286
|
# Every +frequency+ seconds, the reaper will call +reap+ on +pool+.
|
285
|
-
# A reaper instantiated with a nil frequency will never reap the
|
287
|
+
# A reaper instantiated with a +nil+ frequency will never reap the
|
286
288
|
# connection pool.
|
287
289
|
#
|
288
290
|
# Configure the frequency by setting "reaping_frequency" in your
|
@@ -298,7 +300,7 @@ module ActiveRecord
|
|
298
300
|
def run
|
299
301
|
return unless frequency
|
300
302
|
Thread.new(frequency, pool) { |t, p|
|
301
|
-
|
303
|
+
loop do
|
302
304
|
sleep t
|
303
305
|
p.reap
|
304
306
|
end
|
@@ -330,17 +332,17 @@ module ActiveRecord
|
|
330
332
|
# default max pool size to 5
|
331
333
|
@size = (spec.config[:pool] && spec.config[:pool].to_i) || 5
|
332
334
|
|
333
|
-
#
|
334
|
-
#
|
335
|
-
# registry of which thread owns which connection
|
336
|
-
# +connection.owner+ attr on each +connection+ instance.
|
335
|
+
# This variable tracks the cache of threads mapped to reserved connections, with the
|
336
|
+
# sole purpose of speeding up the +connection+ method. It is not the authoritative
|
337
|
+
# registry of which thread owns which connection. Connection ownership is tracked by
|
338
|
+
# the +connection.owner+ attr on each +connection+ instance.
|
337
339
|
# The invariant works like this: if there is mapping of <tt>thread => conn</tt>,
|
338
|
-
# then that +thread+ does indeed own that +conn
|
339
|
-
# mapping does not mean that the +thread+ doesn't own the said connection
|
340
|
+
# then that +thread+ does indeed own that +conn+. However, an absence of a such
|
341
|
+
# mapping does not mean that the +thread+ doesn't own the said connection. In
|
340
342
|
# that case +conn.owner+ attr should be consulted.
|
341
343
|
# Access and modification of +@thread_cached_conns+ does not require
|
342
344
|
# synchronization.
|
343
|
-
@thread_cached_conns = Concurrent::Map.new(:
|
345
|
+
@thread_cached_conns = Concurrent::Map.new(initial_capacity: @size)
|
344
346
|
|
345
347
|
@connections = []
|
346
348
|
@automatic_reconnect = true
|
@@ -353,6 +355,16 @@ module ActiveRecord
|
|
353
355
|
@threads_blocking_new_connections = 0
|
354
356
|
|
355
357
|
@available = ConnectionLeasingQueue.new self
|
358
|
+
|
359
|
+
@lock_thread = false
|
360
|
+
end
|
361
|
+
|
362
|
+
def lock_thread=(lock_thread)
|
363
|
+
if lock_thread
|
364
|
+
@lock_thread = Thread.current
|
365
|
+
else
|
366
|
+
@lock_thread = nil
|
367
|
+
end
|
356
368
|
end
|
357
369
|
|
358
370
|
# Retrieve the connection associated with the current thread, or call
|
@@ -361,13 +373,13 @@ module ActiveRecord
|
|
361
373
|
# #connection can be called any number of times; the connection is
|
362
374
|
# held in a cache keyed by a thread.
|
363
375
|
def connection
|
364
|
-
@thread_cached_conns[connection_cache_key(Thread.current)] ||= checkout
|
376
|
+
@thread_cached_conns[connection_cache_key(@lock_thread || Thread.current)] ||= checkout
|
365
377
|
end
|
366
378
|
|
367
|
-
#
|
379
|
+
# Returns true if there is an open connection being used for the current thread.
|
368
380
|
#
|
369
381
|
# This method only works for connections that have been obtained through
|
370
|
-
# #connection or #with_connection methods
|
382
|
+
# #connection or #with_connection methods. Connections obtained through
|
371
383
|
# #checkout will not be detected by #active_connection?
|
372
384
|
def active_connection?
|
373
385
|
@thread_cached_conns[connection_cache_key(Thread.current)]
|
@@ -429,9 +441,9 @@ module ActiveRecord
|
|
429
441
|
|
430
442
|
# Disconnects all connections in the pool, and clears the pool.
|
431
443
|
#
|
432
|
-
# The pool first tries to gain ownership of all connections
|
444
|
+
# The pool first tries to gain ownership of all connections. If unable to
|
433
445
|
# do so within a timeout interval (default duration is
|
434
|
-
# <tt>spec.config[:checkout_timeout] * 2</tt> seconds), the pool is forcefully
|
446
|
+
# <tt>spec.config[:checkout_timeout] * 2</tt> seconds), then the pool is forcefully
|
435
447
|
# disconnected without any regard for other connection owning threads.
|
436
448
|
def disconnect!
|
437
449
|
disconnect(false)
|
@@ -463,9 +475,9 @@ module ActiveRecord
|
|
463
475
|
# Clears the cache which maps classes and re-connects connections that
|
464
476
|
# require reloading.
|
465
477
|
#
|
466
|
-
# The pool first tries to gain ownership of all connections
|
478
|
+
# The pool first tries to gain ownership of all connections. If unable to
|
467
479
|
# do so within a timeout interval (default duration is
|
468
|
-
# <tt>spec.config[:checkout_timeout] * 2</tt> seconds), the pool forcefully
|
480
|
+
# <tt>spec.config[:checkout_timeout] * 2</tt> seconds), then the pool forcefully
|
469
481
|
# clears the cache and reloads connections without any regard for other
|
470
482
|
# connection owning threads.
|
471
483
|
def clear_reloadable_connections!
|
@@ -496,14 +508,16 @@ module ActiveRecord
|
|
496
508
|
# +conn+: an AbstractAdapter object, which was obtained by earlier by
|
497
509
|
# calling #checkout on this pool.
|
498
510
|
def checkin(conn)
|
499
|
-
synchronize do
|
500
|
-
|
511
|
+
conn.lock.synchronize do
|
512
|
+
synchronize do
|
513
|
+
remove_connection_from_thread_cache conn
|
501
514
|
|
502
|
-
|
503
|
-
|
504
|
-
|
515
|
+
conn._run_checkin_callbacks do
|
516
|
+
conn.expire
|
517
|
+
end
|
505
518
|
|
506
|
-
|
519
|
+
@available.add conn
|
520
|
+
end
|
507
521
|
end
|
508
522
|
end
|
509
523
|
|
@@ -519,20 +533,20 @@ module ActiveRecord
|
|
519
533
|
@available.delete conn
|
520
534
|
|
521
535
|
# @available.any_waiting? => true means that prior to removing this
|
522
|
-
# conn, the pool was at its max size (@connections.size == @size)
|
523
|
-
#
|
536
|
+
# conn, the pool was at its max size (@connections.size == @size).
|
537
|
+
# This would mean that any threads stuck waiting in the queue wouldn't
|
524
538
|
# know they could checkout_new_connection, so let's do it for them.
|
525
539
|
# Because condition-wait loop is encapsulated in the Queue class
|
526
540
|
# (that in turn is oblivious to ConnectionPool implementation), threads
|
527
|
-
# that are "stuck" there are helpless
|
541
|
+
# that are "stuck" there are helpless. They have no way of creating
|
528
542
|
# new connections and are completely reliant on us feeding available
|
529
543
|
# connections into the Queue.
|
530
544
|
needs_new_connection = @available.any_waiting?
|
531
545
|
end
|
532
546
|
|
533
547
|
# This is intentionally done outside of the synchronized section as we
|
534
|
-
# would like not to hold the main mutex while checking out new connections
|
535
|
-
#
|
548
|
+
# would like not to hold the main mutex while checking out new connections.
|
549
|
+
# Thus there is some chance that needs_new_connection information is now
|
536
550
|
# stale, we can live with that (bulk_make_new_connections will make
|
537
551
|
# sure not to exceed the pool's @size limit).
|
538
552
|
bulk_make_new_connections(1) if needs_new_connection
|
@@ -564,225 +578,243 @@ module ActiveRecord
|
|
564
578
|
@available.num_waiting
|
565
579
|
end
|
566
580
|
|
567
|
-
|
568
|
-
|
569
|
-
#
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
# https://github.com/rails/rails/pull/14938#commitcomment-6601951
|
583
|
-
# This hook-in method allows for easier monkey-patching fixes needed by
|
584
|
-
# JRuby users that use Fibers.
|
585
|
-
def connection_cache_key(thread)
|
586
|
-
thread
|
587
|
-
end
|
588
|
-
|
589
|
-
# Take control of all existing connections so a "group" action such as
|
590
|
-
# reload/disconnect can be performed safely. It is no longer enough to
|
591
|
-
# wrap it in +synchronize+ because some pool's actions are allowed
|
592
|
-
# to be performed outside of the main +synchronize+ block.
|
593
|
-
def with_exclusively_acquired_all_connections(raise_on_acquisition_timeout = true)
|
594
|
-
with_new_connections_blocked do
|
595
|
-
attempt_to_checkout_all_existing_connections(raise_on_acquisition_timeout)
|
596
|
-
yield
|
581
|
+
# Return connection pool's usage statistic
|
582
|
+
# Example:
|
583
|
+
#
|
584
|
+
# ActiveRecord::Base.connection_pool.stat # => { size: 15, connections: 1, busy: 1, dead: 0, idle: 0, waiting: 0, checkout_timeout: 5 }
|
585
|
+
def stat
|
586
|
+
synchronize do
|
587
|
+
{
|
588
|
+
size: size,
|
589
|
+
connections: @connections.size,
|
590
|
+
busy: @connections.count { |c| c.in_use? && c.owner.alive? },
|
591
|
+
dead: @connections.count { |c| c.in_use? && !c.owner.alive? },
|
592
|
+
idle: @connections.count { |c| !c.in_use? },
|
593
|
+
waiting: num_waiting_in_queue,
|
594
|
+
checkout_timeout: checkout_timeout
|
595
|
+
}
|
597
596
|
end
|
598
597
|
end
|
599
598
|
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
@available.with_a_bias_for(Thread.current) do
|
610
|
-
while true
|
611
|
-
synchronize do
|
612
|
-
return if collected_conns.size == @connections.size && @now_connecting == 0
|
613
|
-
remaining_timeout = timeout_time - Time.now
|
614
|
-
remaining_timeout = 0 if remaining_timeout < 0
|
615
|
-
conn = checkout_for_exclusive_access(remaining_timeout)
|
616
|
-
collected_conns << conn
|
617
|
-
newly_checked_out << conn
|
599
|
+
private
|
600
|
+
#--
|
601
|
+
# this is unfortunately not concurrent
|
602
|
+
def bulk_make_new_connections(num_new_conns_needed)
|
603
|
+
num_new_conns_needed.times do
|
604
|
+
# try_to_checkout_new_connection will not exceed pool's @size limit
|
605
|
+
if new_conn = try_to_checkout_new_connection
|
606
|
+
# make the new_conn available to the starving threads stuck @available Queue
|
607
|
+
checkin(new_conn)
|
618
608
|
end
|
619
609
|
end
|
620
610
|
end
|
621
|
-
rescue ExclusiveConnectionTimeoutError
|
622
|
-
# <tt>raise_on_acquisition_timeout == false</tt> means we are directed to ignore any
|
623
|
-
# timeouts and are expected to just give up: we've obtained as many connections
|
624
|
-
# as possible, note that in a case like that we don't return any of the
|
625
|
-
# +newly_checked_out+ connections.
|
626
611
|
|
627
|
-
|
628
|
-
|
629
|
-
|
612
|
+
#--
|
613
|
+
# From the discussion on GitHub:
|
614
|
+
# https://github.com/rails/rails/pull/14938#commitcomment-6601951
|
615
|
+
# This hook-in method allows for easier monkey-patching fixes needed by
|
616
|
+
# JRuby users that use Fibers.
|
617
|
+
def connection_cache_key(thread)
|
618
|
+
thread
|
630
619
|
end
|
631
|
-
|
632
|
-
#
|
633
|
-
#
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
620
|
+
|
621
|
+
# Take control of all existing connections so a "group" action such as
|
622
|
+
# reload/disconnect can be performed safely. It is no longer enough to
|
623
|
+
# wrap it in +synchronize+ because some pool's actions are allowed
|
624
|
+
# to be performed outside of the main +synchronize+ block.
|
625
|
+
def with_exclusively_acquired_all_connections(raise_on_acquisition_timeout = true)
|
626
|
+
with_new_connections_blocked do
|
627
|
+
attempt_to_checkout_all_existing_connections(raise_on_acquisition_timeout)
|
628
|
+
yield
|
629
|
+
end
|
641
630
|
end
|
642
|
-
end
|
643
631
|
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
# this block can't be easily moved into attempt_to_checkout_all_existing_connections's
|
650
|
-
# rescue block, because doing so would put it outside of synchronize section, without
|
651
|
-
# being in a critical section thread_report might become inaccurate
|
652
|
-
msg = "could not obtain ownership of all database connections in #{checkout_timeout} seconds"
|
632
|
+
def attempt_to_checkout_all_existing_connections(raise_on_acquisition_timeout = true)
|
633
|
+
collected_conns = synchronize do
|
634
|
+
# account for our own connections
|
635
|
+
@connections.select { |conn| conn.owner == Thread.current }
|
636
|
+
end
|
653
637
|
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
638
|
+
newly_checked_out = []
|
639
|
+
timeout_time = Time.now + (@checkout_timeout * 2)
|
640
|
+
|
641
|
+
@available.with_a_bias_for(Thread.current) do
|
642
|
+
loop do
|
643
|
+
synchronize do
|
644
|
+
return if collected_conns.size == @connections.size && @now_connecting == 0
|
645
|
+
remaining_timeout = timeout_time - Time.now
|
646
|
+
remaining_timeout = 0 if remaining_timeout < 0
|
647
|
+
conn = checkout_for_exclusive_access(remaining_timeout)
|
648
|
+
collected_conns << conn
|
649
|
+
newly_checked_out << conn
|
650
|
+
end
|
651
|
+
end
|
652
|
+
end
|
653
|
+
rescue ExclusiveConnectionTimeoutError
|
654
|
+
# <tt>raise_on_acquisition_timeout == false</tt> means we are directed to ignore any
|
655
|
+
# timeouts and are expected to just give up: we've obtained as many connections
|
656
|
+
# as possible, note that in a case like that we don't return any of the
|
657
|
+
# +newly_checked_out+ connections.
|
658
|
+
|
659
|
+
if raise_on_acquisition_timeout
|
660
|
+
release_newly_checked_out = true
|
661
|
+
raise
|
662
|
+
end
|
663
|
+
rescue Exception # if something else went wrong
|
664
|
+
# this can't be a "naked" rescue, because we have should return conns
|
665
|
+
# even for non-StandardErrors
|
666
|
+
release_newly_checked_out = true
|
667
|
+
raise
|
668
|
+
ensure
|
669
|
+
if release_newly_checked_out && newly_checked_out
|
670
|
+
# releasing only those conns that were checked out in this method, conns
|
671
|
+
# checked outside this method (before it was called) are not for us to release
|
672
|
+
newly_checked_out.each { |conn| checkin(conn) }
|
658
673
|
end
|
659
674
|
end
|
660
675
|
|
661
|
-
|
676
|
+
#--
|
677
|
+
# Must be called in a synchronize block.
|
678
|
+
def checkout_for_exclusive_access(checkout_timeout)
|
679
|
+
checkout(checkout_timeout)
|
680
|
+
rescue ConnectionTimeoutError
|
681
|
+
# this block can't be easily moved into attempt_to_checkout_all_existing_connections's
|
682
|
+
# rescue block, because doing so would put it outside of synchronize section, without
|
683
|
+
# being in a critical section thread_report might become inaccurate
|
684
|
+
msg = "could not obtain ownership of all database connections in #{checkout_timeout} seconds"
|
685
|
+
|
686
|
+
thread_report = []
|
687
|
+
@connections.each do |conn|
|
688
|
+
unless conn.owner == Thread.current
|
689
|
+
thread_report << "#{conn} is owned by #{conn.owner}"
|
690
|
+
end
|
691
|
+
end
|
662
692
|
|
663
|
-
|
664
|
-
end
|
693
|
+
msg << " (#{thread_report.join(', ')})" if thread_report.any?
|
665
694
|
|
666
|
-
|
667
|
-
synchronize do
|
668
|
-
@threads_blocking_new_connections += 1
|
695
|
+
raise ExclusiveConnectionTimeoutError, msg
|
669
696
|
end
|
670
697
|
|
671
|
-
|
672
|
-
|
673
|
-
|
698
|
+
def with_new_connections_blocked
|
699
|
+
synchronize do
|
700
|
+
@threads_blocking_new_connections += 1
|
701
|
+
end
|
674
702
|
|
675
|
-
|
676
|
-
|
703
|
+
yield
|
704
|
+
ensure
|
705
|
+
num_new_conns_required = 0
|
677
706
|
|
678
|
-
|
679
|
-
@
|
707
|
+
synchronize do
|
708
|
+
@threads_blocking_new_connections -= 1
|
680
709
|
|
681
|
-
|
710
|
+
if @threads_blocking_new_connections.zero?
|
711
|
+
@available.clear
|
682
712
|
|
683
|
-
|
684
|
-
|
713
|
+
num_new_conns_required = num_waiting_in_queue
|
714
|
+
|
715
|
+
@connections.each do |conn|
|
716
|
+
next if conn.in_use?
|
685
717
|
|
686
|
-
|
687
|
-
|
718
|
+
@available.add conn
|
719
|
+
num_new_conns_required -= 1
|
720
|
+
end
|
688
721
|
end
|
689
722
|
end
|
690
|
-
end
|
691
|
-
|
692
|
-
bulk_make_new_connections(num_new_conns_required) if num_new_conns_required > 0
|
693
|
-
end
|
694
723
|
|
695
|
-
|
696
|
-
# from the queue of available connections, 2) creating a new
|
697
|
-
# connection if the pool is not at capacity, 3) waiting on the
|
698
|
-
# queue for a connection to become available.
|
699
|
-
#
|
700
|
-
# Raises:
|
701
|
-
# - ActiveRecord::ConnectionTimeoutError if a connection could not be acquired
|
702
|
-
#
|
703
|
-
#--
|
704
|
-
# Implementation detail: the connection returned by +acquire_connection+
|
705
|
-
# will already be "+connection.lease+ -ed" to the current thread.
|
706
|
-
def acquire_connection(checkout_timeout)
|
707
|
-
# NOTE: we rely on +@available.poll+ and +try_to_checkout_new_connection+ to
|
708
|
-
# +conn.lease+ the returned connection (and to do this in a +synchronized+
|
709
|
-
# section), this is not the cleanest implementation, as ideally we would
|
710
|
-
# <tt>synchronize { conn.lease }</tt> in this method, but by leaving it to +@available.poll+
|
711
|
-
# and +try_to_checkout_new_connection+ we can piggyback on +synchronize+ sections
|
712
|
-
# of the said methods and avoid an additional +synchronize+ overhead.
|
713
|
-
if conn = @available.poll || try_to_checkout_new_connection
|
714
|
-
conn
|
715
|
-
else
|
716
|
-
reap
|
717
|
-
@available.poll(checkout_timeout)
|
724
|
+
bulk_make_new_connections(num_new_conns_required) if num_new_conns_required > 0
|
718
725
|
end
|
719
|
-
end
|
720
726
|
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
+
# Acquire a connection by one of 1) immediately removing one
|
728
|
+
# from the queue of available connections, 2) creating a new
|
729
|
+
# connection if the pool is not at capacity, 3) waiting on the
|
730
|
+
# queue for a connection to become available.
|
731
|
+
#
|
732
|
+
# Raises:
|
733
|
+
# - ActiveRecord::ConnectionTimeoutError if a connection could not be acquired
|
734
|
+
#
|
735
|
+
#--
|
736
|
+
# Implementation detail: the connection returned by +acquire_connection+
|
737
|
+
# will already be "+connection.lease+ -ed" to the current thread.
|
738
|
+
def acquire_connection(checkout_timeout)
|
739
|
+
# NOTE: we rely on +@available.poll+ and +try_to_checkout_new_connection+ to
|
740
|
+
# +conn.lease+ the returned connection (and to do this in a +synchronized+
|
741
|
+
# section). This is not the cleanest implementation, as ideally we would
|
742
|
+
# <tt>synchronize { conn.lease }</tt> in this method, but by leaving it to +@available.poll+
|
743
|
+
# and +try_to_checkout_new_connection+ we can piggyback on +synchronize+ sections
|
744
|
+
# of the said methods and avoid an additional +synchronize+ overhead.
|
745
|
+
if conn = @available.poll || try_to_checkout_new_connection
|
746
|
+
conn
|
747
|
+
else
|
748
|
+
reap
|
749
|
+
@available.poll(checkout_timeout)
|
750
|
+
end
|
751
|
+
end
|
727
752
|
|
728
|
-
|
729
|
-
|
730
|
-
|
753
|
+
#--
|
754
|
+
# if owner_thread param is omitted, this must be called in synchronize block
|
755
|
+
def remove_connection_from_thread_cache(conn, owner_thread = conn.owner)
|
756
|
+
@thread_cached_conns.delete_pair(connection_cache_key(owner_thread), conn)
|
731
757
|
end
|
732
|
-
|
758
|
+
alias_method :release, :remove_connection_from_thread_cache
|
733
759
|
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
# Implementation constraint: a newly established connection returned by this
|
738
|
-
# method must be in the +.leased+ state.
|
739
|
-
def try_to_checkout_new_connection
|
740
|
-
# first in synchronized section check if establishing new conns is allowed
|
741
|
-
# and increment @now_connecting, to prevent overstepping this pool's @size
|
742
|
-
# constraint
|
743
|
-
do_checkout = synchronize do
|
744
|
-
if @threads_blocking_new_connections.zero? && (@connections.size + @now_connecting) < @size
|
745
|
-
@now_connecting += 1
|
760
|
+
def new_connection
|
761
|
+
Base.send(spec.adapter_method, spec.config).tap do |conn|
|
762
|
+
conn.schema_cache = schema_cache.dup if schema_cache
|
746
763
|
end
|
747
764
|
end
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
765
|
+
|
766
|
+
# If the pool is not at a +@size+ limit, establish new connection. Connecting
|
767
|
+
# to the DB is done outside main synchronized section.
|
768
|
+
#--
|
769
|
+
# Implementation constraint: a newly established connection returned by this
|
770
|
+
# method must be in the +.leased+ state.
|
771
|
+
def try_to_checkout_new_connection
|
772
|
+
# first in synchronized section check if establishing new conns is allowed
|
773
|
+
# and increment @now_connecting, to prevent overstepping this pool's @size
|
774
|
+
# constraint
|
775
|
+
do_checkout = synchronize do
|
776
|
+
if @threads_blocking_new_connections.zero? && (@connections.size + @now_connecting) < @size
|
777
|
+
@now_connecting += 1
|
778
|
+
end
|
779
|
+
end
|
780
|
+
if do_checkout
|
781
|
+
begin
|
782
|
+
# if successfully incremented @now_connecting establish new connection
|
783
|
+
# outside of synchronized section
|
784
|
+
conn = checkout_new_connection
|
785
|
+
ensure
|
786
|
+
synchronize do
|
787
|
+
if conn
|
788
|
+
adopt_connection(conn)
|
789
|
+
# returned conn needs to be already leased
|
790
|
+
conn.lease
|
791
|
+
end
|
792
|
+
@now_connecting -= 1
|
759
793
|
end
|
760
|
-
@now_connecting -= 1
|
761
794
|
end
|
762
795
|
end
|
763
796
|
end
|
764
|
-
end
|
765
797
|
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
798
|
+
def adopt_connection(conn)
|
799
|
+
conn.pool = self
|
800
|
+
@connections << conn
|
801
|
+
end
|
770
802
|
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
803
|
+
def checkout_new_connection
|
804
|
+
raise ConnectionNotEstablished unless @automatic_reconnect
|
805
|
+
new_connection
|
806
|
+
end
|
775
807
|
|
776
|
-
|
777
|
-
|
778
|
-
|
808
|
+
def checkout_and_verify(c)
|
809
|
+
c._run_checkout_callbacks do
|
810
|
+
c.verify!
|
811
|
+
end
|
812
|
+
c
|
813
|
+
rescue
|
814
|
+
remove c
|
815
|
+
c.disconnect!
|
816
|
+
raise
|
779
817
|
end
|
780
|
-
c
|
781
|
-
rescue
|
782
|
-
remove c
|
783
|
-
c.disconnect!
|
784
|
-
raise
|
785
|
-
end
|
786
818
|
end
|
787
819
|
|
788
820
|
# ConnectionHandler is a collection of ConnectionPool objects. It is used
|
@@ -830,13 +862,13 @@ module ActiveRecord
|
|
830
862
|
# should use.
|
831
863
|
#
|
832
864
|
# The ConnectionHandler class is not coupled with the Active models, as it has no knowlodge
|
833
|
-
# about the model. The model
|
865
|
+
# about the model. The model needs to pass a specification name to the handler,
|
834
866
|
# in order to lookup the correct connection pool.
|
835
867
|
class ConnectionHandler
|
836
868
|
def initialize
|
837
869
|
# These caches are keyed by spec.name (ConnectionSpecification#name).
|
838
|
-
@owner_to_pool = Concurrent::Map.new(:
|
839
|
-
h[k] = Concurrent::Map.new(:
|
870
|
+
@owner_to_pool = Concurrent::Map.new(initial_capacity: 2) do |h, k|
|
871
|
+
h[k] = Concurrent::Map.new(initial_capacity: 2)
|
840
872
|
end
|
841
873
|
end
|
842
874
|
|
@@ -845,8 +877,26 @@ module ActiveRecord
|
|
845
877
|
end
|
846
878
|
alias :connection_pools :connection_pool_list
|
847
879
|
|
848
|
-
def establish_connection(
|
849
|
-
|
880
|
+
def establish_connection(config)
|
881
|
+
resolver = ConnectionSpecification::Resolver.new(Base.configurations)
|
882
|
+
spec = resolver.spec(config)
|
883
|
+
|
884
|
+
remove_connection(spec.name)
|
885
|
+
|
886
|
+
message_bus = ActiveSupport::Notifications.instrumenter
|
887
|
+
payload = {
|
888
|
+
connection_id: object_id
|
889
|
+
}
|
890
|
+
if spec
|
891
|
+
payload[:spec_name] = spec.name
|
892
|
+
payload[:config] = spec.config
|
893
|
+
end
|
894
|
+
|
895
|
+
message_bus.instrument("!connection.active_record", payload) do
|
896
|
+
owner_to_pool[spec.name] = ConnectionAdapters::ConnectionPool.new(spec)
|
897
|
+
end
|
898
|
+
|
899
|
+
owner_to_pool[spec.name]
|
850
900
|
end
|
851
901
|
|
852
902
|
# Returns true if there are any active connections among the connection
|
@@ -879,9 +929,9 @@ module ActiveRecord
|
|
879
929
|
# for (not necessarily the current class).
|
880
930
|
def retrieve_connection(spec_name) #:nodoc:
|
881
931
|
pool = retrieve_connection_pool(spec_name)
|
882
|
-
raise ConnectionNotEstablished, "No connection pool with
|
932
|
+
raise ConnectionNotEstablished, "No connection pool with '#{spec_name}' found." unless pool
|
883
933
|
conn = pool.connection
|
884
|
-
raise ConnectionNotEstablished, "No connection for #{spec_name} in connection pool" unless conn
|
934
|
+
raise ConnectionNotEstablished, "No connection for '#{spec_name}' in connection pool" unless conn
|
885
935
|
conn
|
886
936
|
end
|
887
937
|
|
@@ -894,7 +944,7 @@ module ActiveRecord
|
|
894
944
|
|
895
945
|
# Remove the connection for this class. This will close the active
|
896
946
|
# connection and the defined connection (if they exist). The result
|
897
|
-
# can be used as an argument for establish_connection, for easily
|
947
|
+
# can be used as an argument for #establish_connection, for easily
|
898
948
|
# re-establishing the connection.
|
899
949
|
def remove_connection(spec_name)
|
900
950
|
if pool = owner_to_pool.delete(spec_name)
|
@@ -904,22 +954,18 @@ module ActiveRecord
|
|
904
954
|
end
|
905
955
|
end
|
906
956
|
|
907
|
-
# Retrieving the connection pool happens a lot so we cache it in @
|
957
|
+
# Retrieving the connection pool happens a lot, so we cache it in @owner_to_pool.
|
908
958
|
# This makes retrieving the connection pool O(1) once the process is warm.
|
909
959
|
# When a connection is established or removed, we invalidate the cache.
|
910
|
-
#
|
911
|
-
# Ideally we would use #fetch here, as class_to_pool[klass] may sometimes be nil.
|
912
|
-
# However, benchmarking (https://gist.github.com/jonleighton/3552829) showed that
|
913
|
-
# #fetch is significantly slower than #[]. So in the nil case, no caching will
|
914
|
-
# take place, but that's ok since the nil case is not the common one that we wish
|
915
|
-
# to optimise for.
|
916
960
|
def retrieve_connection_pool(spec_name)
|
917
961
|
owner_to_pool.fetch(spec_name) do
|
962
|
+
# Check if a connection was previously established in an ancestor process,
|
963
|
+
# which may have been forked.
|
918
964
|
if ancestor_pool = pool_from_any_process_for(spec_name)
|
919
965
|
# A connection was established in an ancestor process that must have
|
920
966
|
# subsequently forked. We can't reuse the connection, but we can copy
|
921
967
|
# the specification and establish a new connection with it.
|
922
|
-
establish_connection(ancestor_pool.spec).tap do |pool|
|
968
|
+
establish_connection(ancestor_pool.spec.to_hash).tap do |pool|
|
923
969
|
pool.schema_cache = ancestor_pool.schema_cache if ancestor_pool.schema_cache
|
924
970
|
end
|
925
971
|
else
|
@@ -930,14 +976,14 @@ module ActiveRecord
|
|
930
976
|
|
931
977
|
private
|
932
978
|
|
933
|
-
|
934
|
-
|
935
|
-
|
979
|
+
def owner_to_pool
|
980
|
+
@owner_to_pool[Process.pid]
|
981
|
+
end
|
936
982
|
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
983
|
+
def pool_from_any_process_for(spec_name)
|
984
|
+
owner_to_pool = @owner_to_pool.values.reverse.find { |v| v[spec_name] }
|
985
|
+
owner_to_pool && owner_to_pool[spec_name]
|
986
|
+
end
|
941
987
|
end
|
942
988
|
end
|
943
989
|
end
|