Permalink
Browse files

proxy body responses so we close database connections after body is f…

…lushed
  • Loading branch information...
1 parent 9490703 commit e5246092d1ce30961af4f7d9b5ad86071298cf1c @tenderlove tenderlove committed Mar 29, 2011
View
@@ -1,5 +1,8 @@
*Rails 3.1.0 (unreleased)*
+* ConnectionManagement middleware is changed to clean up the connection pool
+ after the rack body has been flushed.
+
* Added an update_column method on ActiveRecord. This new method updates a given attribute on an object, skipping validations and callbacks.
It is recommended to use #update_attribute unless you are sure you do not want to execute any callback, including the modification of
the updated_at column. It should not be called on new records.
@@ -417,18 +417,35 @@ def retrieve_connection_pool(klass)
end
class ConnectionManagement
+ class Proxy # :nodoc:
+ attr_reader :body, :testing
+
+ def initialize(body, testing = false)
+ @body = body
+ @testing = testing
+ end
+
+ def each(&block)
+ body.each(&block)
+ end
+
+ def close
+ body.close if body.respond_to?(:close)
+
+ # Don't return connection (and perform implicit rollback) if
+ # this request is a part of integration test
+ ActiveRecord::Base.clear_active_connections! unless testing
+ end
+ end
+
def initialize(app)
@app = app
end
def call(env)
- @app.call(env)
- ensure
- # Don't return connection (and perform implicit rollback) if
- # this request is a part of integration test
- unless env.key?("rack.test")
- ActiveRecord::Base.clear_active_connections!
- end
+ status, headers, body = @app.call(env)
+
+ [status, headers, Proxy.new(body, env.key?('rack.test'))]
end
end
end
@@ -11,7 +11,7 @@ def initialize
def call(env)
@calls << env
- [200, {}, [['hi mom']]]
+ [200, {}, ['hi mom']]
end
end
@@ -32,11 +32,31 @@ def test_app_delegation
assert_equal [@env], @app.calls
end
- test "clears active connections after each call" do
+ def test_connections_are_active_after_call
@management.call(@env)
+ assert ActiveRecord::Base.connection_handler.active_connections?
+ end
+
+ def test_body_responds_to_each
+ _, _, body = @management.call(@env)
+ bits = []
+ body.each { |bit| bits << bit }
+ assert_equal ['hi mom'], bits
+ end
+
+ def test_connections_are_cleared_after_body_close
+ _, _, body = @management.call(@env)
+ body.close
assert !ActiveRecord::Base.connection_handler.active_connections?
end
+ def test_active_connections_are_not_cleared_on_body_close_during_test
+ @env['rack.test'] = true
+ _, _, body = @management.call(@env)
+ body.close
+ assert ActiveRecord::Base.connection_handler.active_connections?
+ end
+
test "doesn't clear active connections when running in a test case" do
@env['rack.test'] = true
@management.call(@env)

0 comments on commit e524609

Please sign in to comment.