Skip to content

Commit 26a4a5d

Browse files
authored
Merge pull request rails#53122 from byroot/release-transactional-fixtures-connections
Make the query cache executor transactional fixtures aware
2 parents 5fc266a + c9c6085 commit 26a4a5d

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,31 @@ def clear
183183
end
184184
end
185185

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+
186211
include MonitorMixin
187212
prepend QueryCache::ConnectionPoolConfiguration
188213
include ConnectionAdapters::AbstractPool

activerecord/lib/active_record/query_cache.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ def self.complete(pools)
4343
pool.disable_query_cache!
4444
pool.clear_query_cache
4545
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
5046
end
5147

5248
def self.install_executor_hooks(executor = ActiveSupport::Executor)

activerecord/lib/active_record/railtie.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ class Railtie < Rails::Railtie # :nodoc:
312312
initializer "active_record.set_executor_hooks" do
313313
ActiveRecord::QueryCache.install_executor_hooks
314314
ActiveRecord::AsynchronousQueriesTracker.install_executor_hooks
315+
ActiveRecord::ConnectionAdapters::ConnectionPool.install_executor_hooks
315316
end
316317

317318
initializer "active_record.add_watchable_files" do |app|

activerecord/test/cases/connection_management_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ def test_connections_are_cleared_after_body_close
5050
assert_not ActiveRecord::Base.connection_handler.active_connections?(:all)
5151
end
5252

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+
5366
def test_active_connections_are_not_cleared_on_body_close_during_transaction
5467
ActiveRecord::Base.transaction do
5568
_, _, body = @management.call(@env)
@@ -123,6 +136,7 @@ def executor
123136
@executor ||= Class.new(ActiveSupport::Executor).tap do |exe|
124137
ActiveRecord::QueryCache.install_executor_hooks(exe)
125138
ActiveRecord::AsynchronousQueriesTracker.install_executor_hooks(exe)
139+
ActiveRecord::ConnectionAdapters::ConnectionPool.install_executor_hooks(exe)
126140
end
127141
end
128142

0 commit comments

Comments
 (0)