Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/never_block/frameworks/activerecord.rb,
lib/active_record/connection_adapters/neverblock_postgresql_adapter.rb,
lib/active_record/connection_adapters/neverblock_mysql_adapter.rb
Overview
Patch ActiveRecord to store transaction depth information in fibers instead of threads. AR does not support nested transactions which makes the job easy. We also need to override the scoped methods to store the scope in the fiber context
Class Method Summary collapse
-
.neverblock_mysql_connection(config) ⇒ Object
Establishes a connection to the database that’s used by all Active Record objects.
-
.neverblock_postgresql_connection(config) ⇒ Object
Establishes a connection to the database that’s used by all Active Record objects.
- .transaction(&block) ⇒ Object
Instance Method Summary collapse
Class Method Details
.neverblock_mysql_connection(config) ⇒ Object
Establishes a connection to the database that’s used by all Active Record objects
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/active_record/connection_adapters/neverblock_mysql_adapter.rb', line 63 def self.neverblock_mysql_connection(config) # :nodoc: config = config.symbolize_keys host = config[:host] port = config[:port] socket = config[:socket] username = config[:username] ? config[:username].to_s : 'root' password = config[:password].to_s size = config[:connections] || 4 if config.has_key?(:database) database = config[:database] else raise ArgumentError, "No database specified. Missing argument: database." end MysqlCompat.define_all_hashes_method! ::ActiveRecord::ConnectionAdapters::NeverBlockMysqlAdapter.new(nil, logger, [size.to_i, host, username, password, database, port, socket, nil], config) end |
.neverblock_postgresql_connection(config) ⇒ Object
Establishes a connection to the database that’s used by all Active Record objects
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/active_record/connection_adapters/neverblock_postgresql_adapter.rb', line 84 def self.neverblock_postgresql_connection(config) # :nodoc: config = config.symbolize_keys host = config[:host] port = config[:port] || 5432 username = config[:username].to_s password = config[:password].to_s size = config[:connections] || 4 if config.has_key?(:database) database = config[:database] else raise ArgumentError, "No database specified. Missing argument: database." end # The postgres drivers don't allow the creation of an unconnected PGconn object, # so just pass a nil connection object for the time being. ::ActiveRecord::ConnectionAdapters::NeverBlockPostgreSQLAdapter.new(nil, logger, [size, host, port, nil, nil, database, username, password], config) end |
.transaction(&block) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/never_block/frameworks/activerecord.rb', line 15 def self.transaction(&block) increment_open_transactions begin connection.transaction(Fiber.current['start_db_transaction'], &block) ensure decrement_open_transactions end end |
Instance Method Details
#single_threaded_scoped_methods ⇒ Object
:nodoc:
10 11 12 13 |
# File 'lib/never_block/frameworks/activerecord.rb', line 10 def single_threaded_scoped_methods #:nodoc: scoped_methods = (Fiber.current[:scoped_methods] ||= {}) scoped_methods[self] ||= [] end |