Skip to content

Commit ffb3a3e

Browse files
authored
Merge pull request rails#43674 from Shopify/activerecord-isolated-state
Use `IsolatedExecutionState` across Active Record
2 parents ce3a73a + 703baa6 commit ffb3a3e

File tree

6 files changed

+16
-18
lines changed

6 files changed

+16
-18
lines changed

activerecord/lib/active_record/connection_adapters/abstract/connection_handler.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ def initialize
8181
end
8282

8383
def prevent_writes # :nodoc:
84-
Thread.current[:prevent_writes]
84+
ActiveSupport::IsolatedExecutionState[:active_record_prevent_writes]
8585
end
8686

8787
def prevent_writes=(prevent_writes) # :nodoc:
88-
Thread.current[:prevent_writes] = prevent_writes
88+
ActiveSupport::IsolatedExecutionState[:active_record_prevent_writes] = prevent_writes
8989
end
9090

9191
# Prevent writing to the database regardless of role.

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def prepared_statements?
178178
alias :prepared_statements :prepared_statements?
179179

180180
def prepared_statements_disabled_cache # :nodoc:
181-
Thread.current[:ar_prepared_statements_disabled_cache] ||= Set.new
181+
ActiveSupport::IsolatedExecutionState[:active_record_prepared_statements_disabled_cache] ||= Set.new
182182
end
183183

184184
class Version

activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def begin_isolated_db_transaction(isolation) # :nodoc:
7878
raise TransactionIsolationError, "SQLite3 only supports the `read_uncommitted` transaction isolation level" if isolation != :read_uncommitted
7979
raise StandardError, "You need to enable the shared-cache mode in SQLite mode before attempting to change the transaction isolation level" unless shared_cache?
8080

81-
Thread.current.thread_variable_set("read_uncommitted", @connection.get_first_value("PRAGMA read_uncommitted"))
81+
ActiveSupport::IsolatedExecutionState[:active_record_read_uncommitted] = @connection.get_first_value("PRAGMA read_uncommitted")
8282
@connection.read_uncommitted = true
8383
begin_db_transaction
8484
end
@@ -108,7 +108,7 @@ def high_precision_current_timestamp
108108

109109
private
110110
def reset_read_uncommitted
111-
read_uncommitted = Thread.current.thread_variable_get("read_uncommitted")
111+
read_uncommitted = ActiveSupport::IsolatedExecutionState[:active_record_read_uncommitted]
112112
return unless read_uncommitted
113113

114114
@connection.read_uncommitted = read_uncommitted

activerecord/lib/active_record/connection_handling.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,16 @@ def connecting_to(role: default_role, shard: default_shard, prevent_writes: fals
212212
# is useful in cases you're using sharding to provide per-request
213213
# database isolation.
214214
def prohibit_shard_swapping(enabled = true)
215-
prev_value = Thread.current.thread_variable_get(:prohibit_shard_swapping)
216-
217-
Thread.current.thread_variable_set(:prohibit_shard_swapping, enabled)
218-
215+
prev_value = ActiveSupport::IsolatedExecutionState[:active_record_prohibit_shard_swapping]
216+
ActiveSupport::IsolatedExecutionState[:active_record_prohibit_shard_swapping] = enabled
219217
yield
220218
ensure
221-
Thread.current.thread_variable_set(:prohibit_shard_swapping, prev_value)
219+
ActiveSupport::IsolatedExecutionState[:active_record_prohibit_shard_swapping] = prev_value
222220
end
223221

224222
# Determine whether or not shard swapping is currently prohibited
225223
def shard_swapping_prohibited?
226-
Thread.current.thread_variable_get(:prohibit_shard_swapping)
224+
ActiveSupport::IsolatedExecutionState[:active_record_prohibit_shard_swapping]
227225
end
228226

229227
# Prevent writing to the database regardless of role.

activerecord/lib/active_record/core.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ def self.application_record_class? # :nodoc:
9292
self.filter_attributes = []
9393

9494
def self.connection_handler
95-
Thread.current.thread_variable_get(:ar_connection_handler) || default_connection_handler
95+
ActiveSupport::IsolatedExecutionState[:active_record_connection_handler] || default_connection_handler
9696
end
9797

9898
def self.connection_handler=(handler)
99-
Thread.current.thread_variable_set(:ar_connection_handler, handler)
99+
ActiveSupport::IsolatedExecutionState[:active_record_connection_handler] = handler
100100
end
101101

102102
def self.connection_handlers
@@ -131,8 +131,8 @@ def self.asynchronous_queries_session # :nodoc:
131131
end
132132

133133
def self.asynchronous_queries_tracker # :nodoc:
134-
Thread.current.thread_variable_get(:ar_asynchronous_queries_tracker) ||
135-
Thread.current.thread_variable_set(:ar_asynchronous_queries_tracker, AsynchronousQueriesTracker.new)
134+
ActiveSupport::IsolatedExecutionState[:active_record_asynchronous_queries_tracker] ||= \
135+
AsynchronousQueriesTracker.new
136136
end
137137

138138
# Returns the symbol representing the current connected role.
@@ -199,11 +199,11 @@ def self.current_preventing_writes
199199
end
200200

201201
def self.connected_to_stack # :nodoc:
202-
if connected_to_stack = Thread.current.thread_variable_get(:ar_connected_to_stack)
202+
if connected_to_stack = ActiveSupport::IsolatedExecutionState[:active_record_connected_to_stack]
203203
connected_to_stack
204204
else
205205
connected_to_stack = Concurrent::Array.new
206-
Thread.current.thread_variable_set(:ar_connected_to_stack, connected_to_stack)
206+
ActiveSupport::IsolatedExecutionState[:active_record_connected_to_stack] = connected_to_stack
207207
connected_to_stack
208208
end
209209
end

activerecord/lib/active_record/no_touching.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def applied_to?(klass) # :nodoc:
3939

4040
private
4141
def klasses
42-
Thread.current[:no_touching_classes] ||= []
42+
ActiveSupport::IsolatedExecutionState[:active_record_no_touching_classes] ||= []
4343
end
4444
end
4545

0 commit comments

Comments
 (0)