diff --git a/app/models/solid_cache/entry.rb b/app/models/solid_cache/entry.rb index f8478b5..3203aa1 100644 --- a/app/models/solid_cache/entry.rb +++ b/app/models/solid_cache/entry.rb @@ -41,7 +41,9 @@ def read_multi(keys) keys.each_slice(MULTI_BATCH_SIZE).each do |keys_batch| query = Arel.sql(select_sql(keys_batch), *key_hashes_for(keys_batch)) - results.merge!(connection.select_all(query, "SolidCache::Entry Load").cast_values(attribute_types).to_h) + with_connection do |connection| + results.merge!(connection.select_all(query, "SolidCache::Entry Load").cast_values(attribute_types).to_h) + end end end end @@ -54,7 +56,9 @@ def delete_by_key(*keys) end def clear_truncate - connection.truncate(table_name) + with_connection do |connection| + connection.truncate(table_name) + end end def clear_delete @@ -91,7 +95,9 @@ def add_key_hash_and_byte_size(payloads) end def upsert_unique_by - connection.supports_insert_conflict_target? ? :key_hash : nil + with_connection do |connection| + connection.supports_insert_conflict_target? ? :key_hash : nil + end end # This constructs and caches a SQL query for a given number of keys. diff --git a/app/models/solid_cache/record.rb b/app/models/solid_cache/record.rb index 9d20780..9ec2586 100644 --- a/app/models/solid_cache/record.rb +++ b/app/models/solid_cache/record.rb @@ -14,14 +14,16 @@ def disable_instrumentation(&block) end def with_instrumenter(instrumenter, &block) - if connection.respond_to?(:with_instrumenter) - connection.with_instrumenter(instrumenter, &block) - else - begin - old_instrumenter, ActiveSupport::IsolatedExecutionState[:active_record_instrumenter] = ActiveSupport::IsolatedExecutionState[:active_record_instrumenter], instrumenter - block.call - ensure - ActiveSupport::IsolatedExecutionState[:active_record_instrumenter] = old_instrumenter + with_connection do |connection| + if connection.respond_to?(:with_instrumenter) + connection.with_instrumenter(instrumenter, &block) + else + begin + old_instrumenter, ActiveSupport::IsolatedExecutionState[:active_record_instrumenter] = ActiveSupport::IsolatedExecutionState[:active_record_instrumenter], instrumenter + block.call + ensure + ActiveSupport::IsolatedExecutionState[:active_record_instrumenter] = old_instrumenter + end end end end diff --git a/lib/solid_cache/engine.rb b/lib/solid_cache/engine.rb index da46138..4a0a6d1 100644 --- a/lib/solid_cache/engine.rb +++ b/lib/solid_cache/engine.rb @@ -41,7 +41,7 @@ class Engine < ::Rails::Engine end config.after_initialize do - if SolidCache.configuration.encrypt? && Record.connection.adapter_name == "PostgreSQL" && Rails::VERSION::MAJOR <= 7 + if SolidCache.configuration.encrypt? && Record.lease_connection.adapter_name == "PostgreSQL" && Rails::VERSION::MAJOR <= 7 raise \ "Cannot enable encryption for Solid Cache: in Rails 7, Active Record Encryption does not support " \ "encrypting binary columns on PostgreSQL" diff --git a/test/unit/execution_test.rb b/test/unit/execution_test.rb index 0bbb22e..bd2a238 100644 --- a/test/unit/execution_test.rb +++ b/test/unit/execution_test.rb @@ -106,7 +106,7 @@ def test_active_record_instrumention_expiry end def test_no_connections_uninstrumented - ActiveRecord::ConnectionAdapters::ConnectionPool.any_instance.stubs(connection).raises(ActiveRecord::StatementTimeout) + ActiveRecord::ConnectionAdapters::ConnectionPool.any_instance.stubs(:with_connection).raises(ActiveRecord::StatementTimeout) cache = lookup_store(expires_in: 60, active_record_instrumentation: false) @@ -120,7 +120,7 @@ def test_no_connections_uninstrumented end def test_no_connections_instrumented - ActiveRecord::ConnectionAdapters::ConnectionPool.any_instance.stubs(connection).raises(ActiveRecord::StatementTimeout) + ActiveRecord::ConnectionAdapters::ConnectionPool.any_instance.stubs(:with_connection).raises(ActiveRecord::StatementTimeout) cache = lookup_store(expires_in: 60)