Skip to content
Browse files

Restore connection_id on error.

  • Loading branch information...
1 parent 1b2a7ba commit 8ca93c78832f4fdc78320e3f73ffd07bb71ce196 @kennyj kennyj committed Jul 23, 2012
Showing with 22 additions and 4 deletions.
  1. +10 −4 activerecord/lib/active_record/query_cache.rb
  2. +12 −0 activerecord/test/cases/query_cache_test.rb
View
14 activerecord/lib/active_record/query_cache.rb
@@ -34,16 +34,22 @@ def call(env)
response = @app.call(env)
response[2] = Rack::BodyProxy.new(response[2]) do
- ActiveRecord::Base.connection_id = connection_id
- ActiveRecord::Base.connection.clear_query_cache
- ActiveRecord::Base.connection.disable_query_cache! unless enabled
+ restore_query_cache_settings(connection_id, enabled)
end
response
rescue Exception => e
+ restore_query_cache_settings(connection_id, enabled)
+ raise e
+ end
+
+ private
+
+ def restore_query_cache_settings(connection_id, enabled)
+ ActiveRecord::Base.connection_id = connection_id
ActiveRecord::Base.connection.clear_query_cache
ActiveRecord::Base.connection.disable_query_cache! unless enabled
- raise e
end
+
end
end
View
12 activerecord/test/cases/query_cache_test.rb
@@ -39,6 +39,18 @@ def test_exceptional_middleware_leaves_enabled_cache_alone
assert ActiveRecord::Base.connection.query_cache_enabled, 'cache on'
end
+ def test_exceptional_middleware_assigns_original_connection_id_on_error
+ connection_id = ActiveRecord::Base.connection_id
+
+ mw = ActiveRecord::QueryCache.new lambda { |env|
+ ActiveRecord::Base.connection_id = self.object_id
+ raise "lol borked"
+ }
+ assert_raises(RuntimeError) { mw.call({}) }
+
+ assert_equal connection_id, ActiveRecord::Base.connection_id
+ end
+
def test_middleware_delegates
called = false
mw = ActiveRecord::QueryCache.new lambda { |env|

0 comments on commit 8ca93c7

Please sign in to comment.
Something went wrong with that request. Please try again.