Skip to content

Commit

Permalink
Avoid wasting one connection for default database connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Staubo committed May 19, 2011
1 parent 275e108 commit a939ef6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lib/multidb/balancer.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
module Multidb

class Candidate
def initialize(config)
adapter = config[:adapter]
begin
require "active_record/connection_adapters/#{adapter}_adapter"
rescue LoadError
raise "Please install the #{adapter} adapter: `gem install activerecord-#{adapter}-adapter` (#{$!})"
def initialize(target)
if target.is_a?(Hash)
adapter = target[:adapter]
begin
require "active_record/connection_adapters/#{adapter}_adapter"
rescue LoadError
raise "Please install the #{adapter} adapter: `gem install activerecord-#{adapter}-adapter` (#{$!})"
end
@connection_pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(
ActiveRecord::Base::ConnectionSpecification.new(target, "#{adapter}_connection"))
else
@connection_pool = target
end
@connection_pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(
ActiveRecord::Base::ConnectionSpecification.new(config, "#{adapter}_connection"))
end

def connection
Expand All @@ -30,7 +34,7 @@ def initialize(configuration)
@candidates[name].push(candidate)
end
end
@default_candidate = Candidate.new(@configuration.default_adapter)
@default_candidate = Candidate.new(@configuration.default_pool)
unless @candidates.include?(:default)
@candidates[:default] = [@default_candidate]
end
Expand Down
2 changes: 2 additions & 0 deletions lib/multidb/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ def configure!

class Configuration
def initialize(default_adapter, configuration_hash)
@default_pool = ActiveRecord::Base.connection_pool
@default_adapter = default_adapter
@raw_configuration = configuration_hash
end

attr_reader :default_pool
attr_reader :default_adapter
attr_reader :raw_configuration
end
Expand Down

0 comments on commit a939ef6

Please sign in to comment.