@@ -176,13 +176,13 @@ def initialize(pool_config)
176
176
# #connection can be called any number of times; the connection is
177
177
# held in a cache keyed by a thread.
178
178
def connection
179
- @thread_cached_conns [ connection_cache_key ( ActiveSupport ::IsolatedExecutionState . context ) ] ||= checkout
179
+ @thread_cached_conns [ ActiveSupport ::IsolatedExecutionState . context ] ||= checkout
180
180
end
181
181
182
182
def pin_connection! ( lock_thread ) # :nodoc:
183
183
raise "There is already a pinned connection" if @pinned_connection
184
184
185
- @pinned_connection = ( @thread_cached_conns [ connection_cache_key ( ActiveSupport ::IsolatedExecutionState . context ) ] || checkout )
185
+ @pinned_connection = ( @thread_cached_conns [ ActiveSupport ::IsolatedExecutionState . context ] || checkout )
186
186
# Any leased connection must be in @connections otherwise
187
187
# some methods like #connected? won't behave correctly
188
188
unless @connections . include? ( @pinned_connection )
@@ -226,7 +226,7 @@ def connection_class # :nodoc:
226
226
# #connection or #with_connection methods. Connections obtained through
227
227
# #checkout will not be detected by #active_connection?
228
228
def active_connection?
229
- @thread_cached_conns [ connection_cache_key ( ActiveSupport ::IsolatedExecutionState . context ) ]
229
+ @thread_cached_conns [ ActiveSupport ::IsolatedExecutionState . context ]
230
230
end
231
231
232
232
# Signal that the thread is finished with the current connection.
@@ -237,7 +237,7 @@ def active_connection?
237
237
# #connection or #with_connection methods, connections obtained through
238
238
# #checkout will not be automatically released.
239
239
def release_connection ( owner_thread = ActiveSupport ::IsolatedExecutionState . context )
240
- if conn = @thread_cached_conns . delete ( connection_cache_key ( owner_thread ) )
240
+ if conn = @thread_cached_conns . delete ( owner_thread )
241
241
checkin conn
242
242
end
243
243
end
@@ -252,7 +252,7 @@ def release_connection(owner_thread = ActiveSupport::IsolatedExecutionState.cont
252
252
# connection will be properly returned to the pool by the code that checked
253
253
# it out.
254
254
def with_connection
255
- unless conn = @thread_cached_conns [ connection_cache_key ( ActiveSupport ::IsolatedExecutionState . context ) ]
255
+ unless conn = @thread_cached_conns [ ActiveSupport ::IsolatedExecutionState . context ]
256
256
conn = connection
257
257
fresh_connection = true
258
258
end
@@ -471,6 +471,8 @@ def reap
471
471
remove conn
472
472
end
473
473
end
474
+
475
+ prune_thread_cache
474
476
end
475
477
476
478
# Disconnect all connections that have been idle for at least
@@ -558,15 +560,6 @@ def bulk_make_new_connections(num_new_conns_needed)
558
560
end
559
561
end
560
562
561
- #--
562
- # From the discussion on GitHub:
563
- # https://github.com/rails/rails/pull/14938#commitcomment-6601951
564
- # This hook-in method allows for easier monkey-patching fixes needed by
565
- # JRuby users that use Fibers.
566
- def connection_cache_key ( thread )
567
- thread
568
- end
569
-
570
563
# Take control of all existing connections so a "group" action such as
571
564
# reload/disconnect can be performed safely. It is no longer enough to
572
565
# wrap it in +synchronize+ because some pool's actions are allowed
@@ -710,10 +703,17 @@ def acquire_connection(checkout_timeout)
710
703
#--
711
704
# if owner_thread param is omitted, this must be called in synchronize block
712
705
def remove_connection_from_thread_cache ( conn , owner_thread = conn . owner )
713
- @thread_cached_conns . delete_pair ( connection_cache_key ( owner_thread ) , conn )
706
+ @thread_cached_conns . delete_pair ( owner_thread , conn )
714
707
end
715
708
alias_method :release , :remove_connection_from_thread_cache
716
709
710
+ def prune_thread_cache
711
+ dead_threads = @thread_cached_conns . keys . reject ( &:alive? )
712
+ dead_threads . each do |dead_thread |
713
+ @thread_cached_conns . delete ( dead_thread )
714
+ end
715
+ end
716
+
717
717
def new_connection
718
718
connection = db_config . new_connection
719
719
connection . pool = self
0 commit comments