Skip to content

Commit 7f65b2a

Browse files
committed
Switch from ActiveRecord connection to with_connection / lease_connection
1 parent 3e4947b commit 7f65b2a

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

app/models/solid_cache/entry.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def read_multi(keys)
4141
keys.each_slice(MULTI_BATCH_SIZE).each do |keys_batch|
4242
query = Arel.sql(select_sql(keys_batch), *key_hashes_for(keys_batch))
4343

44-
results.merge!(connection.select_all(query, "SolidCache::Entry Load").cast_values(attribute_types).to_h)
44+
with_connection do |connection|
45+
results.merge!(connection.select_all(query, "SolidCache::Entry Load").cast_values(attribute_types).to_h)
46+
end
4547
end
4648
end
4749
end
@@ -54,7 +56,9 @@ def delete_by_key(*keys)
5456
end
5557

5658
def clear_truncate
57-
connection.truncate(table_name)
59+
with_connection do |connection|
60+
connection.truncate(table_name)
61+
end
5862
end
5963

6064
def clear_delete
@@ -91,7 +95,9 @@ def add_key_hash_and_byte_size(payloads)
9195
end
9296

9397
def upsert_unique_by
94-
connection.supports_insert_conflict_target? ? :key_hash : nil
98+
with_connection do |connection|
99+
connection.supports_insert_conflict_target? ? :key_hash : nil
100+
end
95101
end
96102

97103
# This constructs and caches a SQL query for a given number of keys.

app/models/solid_cache/record.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ def disable_instrumentation(&block)
1414
end
1515

1616
def with_instrumenter(instrumenter, &block)
17-
if connection.respond_to?(:with_instrumenter)
18-
connection.with_instrumenter(instrumenter, &block)
19-
else
20-
begin
21-
old_instrumenter, ActiveSupport::IsolatedExecutionState[:active_record_instrumenter] = ActiveSupport::IsolatedExecutionState[:active_record_instrumenter], instrumenter
22-
block.call
23-
ensure
24-
ActiveSupport::IsolatedExecutionState[:active_record_instrumenter] = old_instrumenter
17+
with_connection do |connection|
18+
if connection.respond_to?(:with_instrumenter)
19+
connection.with_instrumenter(instrumenter, &block)
20+
else
21+
begin
22+
old_instrumenter, ActiveSupport::IsolatedExecutionState[:active_record_instrumenter] = ActiveSupport::IsolatedExecutionState[:active_record_instrumenter], instrumenter
23+
block.call
24+
ensure
25+
ActiveSupport::IsolatedExecutionState[:active_record_instrumenter] = old_instrumenter
26+
end
2527
end
2628
end
2729
end

lib/solid_cache/engine.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Engine < ::Rails::Engine
4141
end
4242

4343
config.after_initialize do
44-
if SolidCache.configuration.encrypt? && Record.connection.adapter_name == "PostgreSQL" && Rails::VERSION::MAJOR <= 7
44+
if SolidCache.configuration.encrypt? && Record.lease_connection.adapter_name == "PostgreSQL" && Rails::VERSION::MAJOR <= 7
4545
raise \
4646
"Cannot enable encryption for Solid Cache: in Rails 7, Active Record Encryption does not support " \
4747
"encrypting binary columns on PostgreSQL"

test/unit/execution_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_active_record_instrumention_expiry
106106
end
107107

108108
def test_no_connections_uninstrumented
109-
ActiveRecord::ConnectionAdapters::ConnectionPool.any_instance.stubs(connection).raises(ActiveRecord::StatementTimeout)
109+
ActiveRecord::ConnectionAdapters::ConnectionPool.any_instance.stubs(:with_connection).raises(ActiveRecord::StatementTimeout)
110110

111111
cache = lookup_store(expires_in: 60, active_record_instrumentation: false)
112112

@@ -120,7 +120,7 @@ def test_no_connections_uninstrumented
120120
end
121121

122122
def test_no_connections_instrumented
123-
ActiveRecord::ConnectionAdapters::ConnectionPool.any_instance.stubs(connection).raises(ActiveRecord::StatementTimeout)
123+
ActiveRecord::ConnectionAdapters::ConnectionPool.any_instance.stubs(:with_connection).raises(ActiveRecord::StatementTimeout)
124124

125125
cache = lookup_store(expires_in: 60)
126126

0 commit comments

Comments
 (0)