Skip to content

Commit

Permalink
Use ActiveRecord's with_connection when possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Staubo committed May 19, 2011
1 parent a566e36 commit fbdb784
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/multidb/balancer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ def initialize(target)
end
end

def connection
@connection_pool.connection
def connection(&block)
if block_given?
@connection_pool.with_connection(&block)
else
@connection_pool.connection
end
end
end

Expand Down Expand Up @@ -51,18 +55,19 @@ def get(name, &block)
def use(name, &block)
result = nil
get(name) do |candidate|
connection = candidate.connection
if block_given?
previous_connection, Thread.current[:multidb_connection] =
Thread.current[:multidb_connection], connection
begin
result = yield
ensure
Thread.current[:multidb_connection] = previous_connection
candidate.connection do |connection|
previous_connection, Thread.current[:multidb_connection] =
Thread.current[:multidb_connection], connection
begin
result = yield
ensure
Thread.current[:multidb_connection] = previous_connection
end
result
end
result
else
result = Thread.current[:multidb_connection] = connection
result = Thread.current[:multidb_connection] = candidate.connection
end
end
result
Expand Down

0 comments on commit fbdb784

Please sign in to comment.