Module: ActiveRecordHostPool::ClearQueryCachePatch

Defined in:
lib/active_record_host_pool/clear_query_cache_patch.rb

Overview

ActiveRecord 6.0 introduced multiple database support. With that, an update has been made in github.com/rails/rails/pull/35089 to ensure that all query caches are cleared across connection handlers and pools. If you write on one connection, the other connection will have the update that occurred.

This broke ARHP which implements its own pool, allowing you to access multiple databases with the same connection (e.g. 1 connection for 100 shards on the same server).

This patch maintains the reference to the database during the cache clearing to ensure that the database doesn’t get swapped out mid-way into an operation.

This is a private Rails API and may change in future releases as they’re actively working on sharding in Rails 6 and above.

Instance Method Summary collapse

Instance Method Details

#clear_on_handler(handler) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/active_record_host_pool/clear_query_cache_patch.rb', line 30

def clear_on_handler(handler)
  handler.all_connection_pools.each do |pool|
    db_was = pool._unproxied_connection._host_pool_current_database
    pool._unproxied_connection.clear_query_cache if pool.active_connection?
  ensure
    pool._unproxied_connection._host_pool_current_database = db_was
  end
end

#clear_query_caches_for_current_threadObject



22
23
24
25
26
27
28
# File 'lib/active_record_host_pool/clear_query_cache_patch.rb', line 22

def clear_query_caches_for_current_thread
  host_pool_current_database_was = connection_pool._unproxied_connection._host_pool_current_database
  super
ensure
  # restore in case clearing the cache changed the database
  connection_pool._unproxied_connection._host_pool_current_database = host_pool_current_database_was
end