Module: Semian::Mysql2

Includes:
Adapter
Defined in:
lib/semian/mysql2.rb

Constant Summary collapse

CONNECTION_ERROR =
Regexp.union(
  /Can't connect to MySQL server on/i,
  /Lost connection to MySQL server during query/i,
  /MySQL server has gone away/i,
)
ResourceBusyError =
::Mysql2::ResourceBusyError
CircuitOpenError =
::Mysql2::CircuitOpenError
DEFAULT_HOST =
'localhost'
DEFAULT_PORT =
3306
QUERY_WHITELIST =
Regexp.union(
  /\A\s*ROLLBACK/i,
  /\A\s*COMMIT/i,
  /\A\s*RELEASE\s+SAVEPOINT/i,
)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Adapter

#semian_resource

Class Method Details

.included(base) ⇒ Object

The naked methods are exposed as ‘raw_query` and `raw_connect` for instrumentation purpose



42
43
44
45
46
47
48
# File 'lib/semian/mysql2.rb', line 42

def self.included(base)
  base.send(:alias_method, :raw_query, :query)
  base.send(:remove_method, :query)

  base.send(:alias_method, :raw_connect, :connect)
  base.send(:remove_method, :connect)
end

Instance Method Details

#query(*args) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/semian/mysql2.rb', line 61

def query(*args)
  if query_whitelisted?(*args)
    raw_query(*args)
  else
    acquire_semian_resource(adapter: :mysql, scope: :query) { raw_query(*args) }
  end
end

#semian_identifierObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/semian/mysql2.rb', line 50

def semian_identifier
  @semian_identifier ||= begin
    unless name = semian_options && semian_options[:name]
      host = query_options[:host] || DEFAULT_HOST
      port = query_options[:port] || DEFAULT_PORT
      name = "#{host}:#{port}"
    end
    :"mysql_#{name}"
  end
end