File tree Expand file tree Collapse file tree 4 files changed +40
-4
lines changed
connection_adapters/abstract Expand file tree Collapse file tree 4 files changed +40
-4
lines changed Original file line number Diff line number Diff line change @@ -183,6 +183,31 @@ def clear
183
183
end
184
184
end
185
185
186
+ module ExecutorHooks # :nodoc:
187
+ class << self
188
+ def run
189
+ # noop
190
+ end
191
+
192
+ def complete ( _ )
193
+ ActiveRecord ::Base . connection_handler . each_connection_pool do |pool |
194
+ if ( connection = pool . active_connection? )
195
+ transaction = connection . current_transaction
196
+ if transaction . closed? || !transaction . joinable?
197
+ pool . release_connection
198
+ end
199
+ end
200
+ end
201
+ end
202
+ end
203
+ end
204
+
205
+ class << self
206
+ def install_executor_hooks ( executor = ActiveSupport ::Executor )
207
+ executor . register_hook ( ExecutorHooks )
208
+ end
209
+ end
210
+
186
211
include MonitorMixin
187
212
prepend QueryCache ::ConnectionPoolConfiguration
188
213
include ConnectionAdapters ::AbstractPool
Original file line number Diff line number Diff line change @@ -43,10 +43,6 @@ def self.complete(pools)
43
43
pool . disable_query_cache!
44
44
pool . clear_query_cache
45
45
end
46
-
47
- ActiveRecord ::Base . connection_handler . each_connection_pool do |pool |
48
- pool . release_connection if pool . active_connection? && !pool . lease_connection . transaction_open?
49
- end
50
46
end
51
47
52
48
def self . install_executor_hooks ( executor = ActiveSupport ::Executor )
Original file line number Diff line number Diff line change @@ -312,6 +312,7 @@ class Railtie < Rails::Railtie # :nodoc:
312
312
initializer "active_record.set_executor_hooks" do
313
313
ActiveRecord ::QueryCache . install_executor_hooks
314
314
ActiveRecord ::AsynchronousQueriesTracker . install_executor_hooks
315
+ ActiveRecord ::ConnectionAdapters ::ConnectionPool . install_executor_hooks
315
316
end
316
317
317
318
initializer "active_record.add_watchable_files" do |app |
Original file line number Diff line number Diff line change @@ -50,6 +50,19 @@ def test_connections_are_cleared_after_body_close
50
50
assert_not ActiveRecord ::Base . connection_handler . active_connections? ( :all )
51
51
end
52
52
53
+ test "connections are cleared even if inside a non-joinable transaction" do
54
+ ActiveRecord ::Base . connection_pool . pin_connection! ( Thread . current )
55
+ Thread . new do
56
+ assert ActiveRecord ::Base . lease_connection
57
+ assert ActiveRecord ::Base . connection_handler . active_connections? ( :all )
58
+ _ , _ , body = @management . call ( @env )
59
+ body . close
60
+ assert_not ActiveRecord ::Base . connection_handler . active_connections? ( :all )
61
+ end . join
62
+ ensure
63
+ ActiveRecord ::Base . connection_pool . unpin_connection!
64
+ end
65
+
53
66
def test_active_connections_are_not_cleared_on_body_close_during_transaction
54
67
ActiveRecord ::Base . transaction do
55
68
_ , _ , body = @management . call ( @env )
@@ -123,6 +136,7 @@ def executor
123
136
@executor ||= Class . new ( ActiveSupport ::Executor ) . tap do |exe |
124
137
ActiveRecord ::QueryCache . install_executor_hooks ( exe )
125
138
ActiveRecord ::AsynchronousQueriesTracker . install_executor_hooks ( exe )
139
+ ActiveRecord ::ConnectionAdapters ::ConnectionPool . install_executor_hooks ( exe )
126
140
end
127
141
end
128
142
You can’t perform that action at this time.
0 commit comments