Created a runtime registry for thread local variables in active record.
This commit is contained in:
parent
e94f024e14
commit
95ac3913ee
|
@ -50,6 +50,7 @@ module ActiveRecord
|
|||
autoload :Querying
|
||||
autoload :ReadonlyAttributes
|
||||
autoload :Reflection
|
||||
autoload :RuntimeRegistry
|
||||
autoload :Sanitization
|
||||
autoload :Schema
|
||||
autoload :SchemaDumper
|
||||
|
|
|
@ -54,11 +54,11 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def connection_id
|
||||
Thread.current['ActiveRecord::Base.connection_id']
|
||||
ActiveRecord::RuntimeRegistry.instance.connection_id
|
||||
end
|
||||
|
||||
def connection_id=(connection_id)
|
||||
Thread.current['ActiveRecord::Base.connection_id'] = connection_id
|
||||
ActiveRecord::RuntimeRegistry.instance.connection_id = connection_id
|
||||
end
|
||||
|
||||
# Returns the configuration of the associated connection as a hash:
|
||||
|
|
|
@ -80,11 +80,11 @@ module ActiveRecord
|
|||
class_attribute :default_connection_handler, instance_writer: false
|
||||
|
||||
def self.connection_handler
|
||||
Thread.current[:active_record_connection_handler] || self.default_connection_handler
|
||||
ActiveRecord::RuntimeRegistry.instance.connection_handler || self.default_connection_handler
|
||||
end
|
||||
|
||||
def self.connection_handler=(handler)
|
||||
Thread.current[:active_record_connection_handler] = handler
|
||||
ActiveRecord::RuntimeRegistry.instance.connection_handler = handler
|
||||
end
|
||||
|
||||
self.default_connection_handler = ConnectionAdapters::ConnectionHandler.new
|
||||
|
|
|
@ -3,11 +3,11 @@ module ActiveRecord
|
|||
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]
|
||||
|
||||
def self.runtime=(value)
|
||||
Thread.current[:active_record_sql_runtime] = value
|
||||
ActiveRecord::RuntimeRegistry.instance.sql_runtime = value
|
||||
end
|
||||
|
||||
def self.runtime
|
||||
Thread.current[:active_record_sql_runtime] ||= 0
|
||||
ActiveRecord::RuntimeRegistry.instance.sql_runtime ||= 0
|
||||
end
|
||||
|
||||
def self.reset_runtime
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
require 'active_support/per_thread_registry'
|
||||
|
||||
module ActiveRecord
|
||||
# This is a registry class for storing local variables in active record. The
|
||||
# class allows you to access variables that are local to the current thread.
|
||||
# These thread local variables are stored as attributes in the
|
||||
# +RuntimeRegistry+ class.
|
||||
#
|
||||
# You can access the thread local variables by calling a variable's name on
|
||||
# the +RuntimeRegistry+ class. For example, if you wanted to obtain the
|
||||
# connection handler for the current thread, you would invoke:
|
||||
#
|
||||
# ActiveRecord::RuntimeRegistry.instance.connection_handler
|
||||
#
|
||||
# The +PerThreadRegistry+ module will make a new +RuntimeRegistry+ instance
|
||||
# and store it in +Thread.current+. Whenever you make a call for an attribute
|
||||
# on the +RuntimeRegistry+ class, the call will be sent to the instance that
|
||||
# is stored in +Thread.current+.
|
||||
#
|
||||
# Note that you can also make the following call which would provide an
|
||||
# equivalent result as the previous code:
|
||||
#
|
||||
# ActiveRecord::RuntimeRegistry.connection_handler
|
||||
#
|
||||
# However, this is less performant because it makes a call to +method_missing+
|
||||
# before it sends the method call to the +instance+.
|
||||
class RuntimeRegistry
|
||||
extend ActiveSupport::PerThreadRegistry
|
||||
|
||||
attr_accessor :connection_handler, :sql_runtime, :connection_id
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue