|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
| 3 | +require "set" |
3 | 4 | require "active_record/connection_adapters/determine_if_preparable_visitor" |
4 | 5 | require "active_record/connection_adapters/schema_cache" |
5 | 6 | require "active_record/connection_adapters/sql_type_metadata" |
|
10 | 11 | require "arel/collectors/composite" |
11 | 12 | require "arel/collectors/sql_string" |
12 | 13 | require "arel/collectors/substitute_binds" |
13 | | -require "concurrent/atomic/thread_local_var" |
14 | 14 |
|
15 | 15 | module ActiveRecord |
16 | 16 | module ConnectionAdapters # :nodoc: |
@@ -92,10 +92,10 @@ def initialize(connection, logger = nil, config = {}) # :nodoc: |
92 | 92 | @lock = ActiveSupport::Concurrency::LoadInterlockAwareMonitor.new |
93 | 93 |
|
94 | 94 | if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true }) |
95 | | - @prepared_statement_status = Concurrent::ThreadLocalVar.new(true) |
| 95 | + @prepared_statements = true |
96 | 96 | @visitor.extend(DetermineIfPreparableVisitor) |
97 | 97 | else |
98 | | - @prepared_statement_status = Concurrent::ThreadLocalVar.new(false) |
| 98 | + @prepared_statements = false |
99 | 99 | end |
100 | 100 |
|
101 | 101 | @advisory_locks_enabled = self.class.type_cast_config_to_boolean( |
@@ -139,7 +139,11 @@ def schema_migration # :nodoc: |
139 | 139 | end |
140 | 140 |
|
141 | 141 | def prepared_statements |
142 | | - @prepared_statement_status.value |
| 142 | + @prepared_statements && !prepared_statements_disabled_cache.include?(object_id) |
| 143 | + end |
| 144 | + |
| 145 | + def prepared_statements_disabled_cache # :nodoc: |
| 146 | + Thread.current[:ar_prepared_statements_disabled_cache] ||= Set.new |
143 | 147 | end |
144 | 148 |
|
145 | 149 | class Version |
@@ -226,7 +230,10 @@ def seconds_idle # :nodoc: |
226 | 230 | end |
227 | 231 |
|
228 | 232 | def unprepared_statement |
229 | | - @prepared_statement_status.bind(false) { yield } |
| 233 | + cache = prepared_statements_disabled_cache.add(object_id) if @prepared_statements |
| 234 | + yield |
| 235 | + ensure |
| 236 | + cache&.delete(object_id) |
230 | 237 | end |
231 | 238 |
|
232 | 239 | # Returns the human-readable name of the adapter. Use mixed case - one |
|
0 commit comments