Class: Rails::Sharding::ConnectionHandler
- Inherits:
-
Object
- Object
- Rails::Sharding::ConnectionHandler
show all
- Defined in:
- lib/rails/sharding/connection_handler.rb
Defined Under Namespace
Classes: ConnectionPoolOwner
Class Method Summary
collapse
Class Method Details
.connected?(shard_group, shard_name) ⇒ Boolean
52
53
54
|
# File 'lib/rails/sharding/connection_handler.rb', line 52
def self.connected?(shard_group, shard_name)
connection_handler.connected?(connection_pool_owner(shard_group, shard_name))
end
|
.connection_pool(shard_group, shard_name) ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/rails/sharding/connection_handler.rb', line 40
def self.connection_pool(shard_group, shard_name)
connection_handler.retrieve_connection_pool(connection_pool_owner(shard_group, shard_name))
rescue Errors::ConnectionPoolRetrievalError
raise ActiveRecord::ConnectionNotEstablished, "No connection pool for shard #{connection_name(shard_group, shard_name)}"
end
|
.establish_all_connections ⇒ Object
Establishes connections to all shards in all shard groups. Despite the name, this actually only creates a connection pool with zero connections for each shard. The connections will be allocated for each thread when #retrieve_connection or #with_connection are called
8
9
10
11
12
13
14
|
# File 'lib/rails/sharding/connection_handler.rb', line 8
def self.establish_all_connections
Core.shard_groups.each do |shard_group|
Core.shard_names(shard_group).each do |shard_name|
establish_connection(shard_group, shard_name)
end
end
end
|
.establish_connection(shard_group, shard_name, environment = nil) ⇒ Object
Establishes a connection to a single shard in a single shard group
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/rails/sharding/connection_handler.rb', line 17
def self.establish_connection(shard_group, shard_name, environment=nil)
self.setup unless defined? @@connection_handler
unless configurations = (environment.nil? ? Core.configurations : Core.configurations(environment))
raise Errors::ConfigNotFoundError, "Cannot find configuration for environment '#{environment}' in #{Config.shards_config_file}"
end
unless shard_group_configurations = configurations[shard_group.to_s]
raise Errors::ConfigNotFoundError, "Cannot find configuration for shard_group '#{shard_group}' in environment '#{environment}' in #{Config.shards_config_file}"
end
resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(shard_group_configurations)
begin
connection_spec = resolver.spec(shard_name.to_sym)
rescue ActiveRecord::AdapterNotSpecified => e
raise Errors::ConfigNotFoundError, "Cannot find configuration for shard '#{shard_group}:#{shard_name}' in environment '#{environment}' in #{Config.shards_config_file}"
end
connection_handler.establish_connection(connection_pool_owner(shard_group, shard_name), connection_spec)
end
|
.remove_connection(shard_group, shard_name) ⇒ Object
60
61
62
|
# File 'lib/rails/sharding/connection_handler.rb', line 60
def self.remove_connection(shard_group, shard_name)
connection_handler.remove_connection(connection_pool_owner(shard_group, shard_name))
end
|
.retrieve_connection(shard_group, shard_name) ⇒ Object
48
49
50
|
# File 'lib/rails/sharding/connection_handler.rb', line 48
def self.retrieve_connection(shard_group, shard_name)
connection_handler.retrieve_connection(connection_pool_owner(shard_group, shard_name))
end
|
.with_connection(shard_group, shard_name, &block) ⇒ Object
56
57
58
|
# File 'lib/rails/sharding/connection_handler.rb', line 56
def self.with_connection(shard_group, shard_name, &block)
connection_pool(shard_group, shard_name).with_connection(&block)
end
|