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

.decrement_open_transactionsObject

:nodoc:



32
33
34
# File 'lib/never_block/frameworks/activerecord.rb', line 32

def self.decrement_open_transactions #:nodoc:
  Fiber.current['open_transactions'] -= 1
end

.increment_open_transactionsObject

:nodoc:



26
27
28
29
30
# File 'lib/never_block/frameworks/activerecord.rb', line 26

def self.increment_open_transactions #:nodoc:
  open = Fiber.current['open_transactions'] ||= 0
  Fiber.current['start_db_transaction'] = open.zero?
  Fiber.current['open_transactions'] = open + 1
end

.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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/active_record/connection_adapters/neverblock_postgresql_adapter.rb', line 75

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_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