Skip to content

Commit

Permalink
call clear_active_connections! in :after_dispatch to give pooled conn…
Browse files Browse the repository at this point in the history
…ections back

This fixes connection pool exhaustion for web servers which create new thread per connection (e.g. Webrick).

integration.rb changes are required to keep test transaction active for several requests.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1171 state:committed]
  • Loading branch information
Aliaksey Kandratsenka authored and NZKoz committed Oct 4, 2008
1 parent 8343611 commit 6080b73
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions actionpack/lib/action_controller/dispatcher.rb
Expand Up @@ -20,6 +20,7 @@ def define_dispatcher_callbacks(cache_classes)
end

if defined?(ActiveRecord)
after_dispatch :checkin_connections
before_dispatch { ActiveRecord::Base.verify_active_connections! }
to_prepare(:activerecord_instantiate_observers) { ActiveRecord::Base.instantiate_observers }
end
Expand Down Expand Up @@ -145,6 +146,21 @@ def flush_logger
Base.logger.flush
end

def mark_as_test_request!
@test_request = true
self
end

def test_request?
@test_request
end

def checkin_connections
# Don't return connection (and peform implicit rollback) if this request is a part of integration test
return if test_request?
ActiveRecord::Base.clear_active_connections!
end

protected
def handle_request
@controller = Routing::Routes.recognize(@request)
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/integration.rb
Expand Up @@ -276,7 +276,7 @@ def process(method, path, parameters = nil, headers = nil)
ActionController::Base.clear_last_instantiation!

env['rack.input'] = data.is_a?(IO) ? data : StringIO.new(data || '')
@status, @headers, result_body = ActionController::Dispatcher.new.call(env)
@status, @headers, result_body = ActionController::Dispatcher.new.mark_as_test_request!.call(env)
@request_count += 1

@controller = ActionController::Base.last_instantiation
Expand Down

0 comments on commit 6080b73

Please sign in to comment.