Permalink
Browse files

Avoid wasting one connection for default database connection.

  • Loading branch information...
1 parent 275e108 commit a939ef6daafc851daa4a30e7b99b68d495768ae8 Alexander Staubo committed May 19, 2011
Showing with 15 additions and 9 deletions.
  1. +13 −9 lib/multidb/balancer.rb
  2. +2 −0 lib/multidb/configuration.rb
@@ -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
@@ -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
@@ -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

0 comments on commit a939ef6

Please sign in to comment.