Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
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

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/active_record/connection_adapters/neverblock_mysql_adapter.rb', line 51

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/active_record/connection_adapters/neverblock_postgresql_adapter.rb', line 67

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 &block
  ensure
    decrement_open_transactions
  end
end

Instance Method Details

#single_threaded_scoped_methodsObject

: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