Skip to content

Commit

Permalink
Merge pull request rails#51222 from Shopify/ar-clear-pinned-connectio…
Browse files Browse the repository at this point in the history
…n-cache

Clear all threads query cache when a connection is pinned
  • Loading branch information
byroot authored Feb 29, 2024
2 parents 3ecc269 + 1d0b396 commit 1f6cef4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,14 @@ def query_cache_enabled
end

def clear_query_cache
query_cache.clear
if @pinned_connection
# With transactional fixtures, and especially systems test
# another thread may use the same connection, but with a different
# query cache. So we must clear them all.
@thread_query_caches.each_value(&:clear)
else
query_cache.clear
end
end

private
Expand Down
34 changes: 34 additions & 0 deletions activerecord/test/cases/query_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,40 @@ def test_clear_query_cache_is_called_on_all_connections
end
end

test "query cache is cleared for all thread when a connection is shared" do
ActiveRecord::Base.connection_pool.pin_connection!(ActiveSupport::IsolatedExecutionState.context)

begin
assert_cache :off
ActiveRecord::Base.connection.enable_query_cache!
assert_cache :clean

Post.first
assert_cache :dirty

thread_a = Thread.new do
middleware { |env|
assert_cache :dirty # The cache is shared with the main thread

Post.first
assert_cache :dirty

Post.delete_all

assert_cache :clean

[200, {}, nil]
}.call({})
end

thread_a.join

assert_cache :clean
ensure
ActiveRecord::Base.connection_pool.unpin_connection!
end
end

private
def with_temporary_connection_pool(&block)
pool_config = ActiveRecord::Base.connection.pool.pool_config
Expand Down

0 comments on commit 1f6cef4

Please sign in to comment.