Skip to content

Commit

Permalink
Fixed query cache when multiple database connections were involved
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6195 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
Tobias Lütke committed Feb 21, 2007
1 parent 2ffbc61 commit 7842cae
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/query_cache.rb
Expand Up @@ -73,13 +73,13 @@ class << self
alias_method :connection_without_query_cache, :connection

def query_caches
(Thread.current[:query_cache] ||= {})
Thread.current["query_cache_#{connection_without_query_cache.object_id}"] ||= {}
end

def query_cache
if query_caches[self]
query_caches[self]
elsif superclass.respond_to?(:query_cache)
elsif superclass.respond_to?(:query_cache) and superclass.respond_to?(:connection) and superclass.connection_without_query_cache == connection_without_query_cache
superclass.query_cache
end
end
Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/query_cache_test.rb
Expand Up @@ -2,6 +2,7 @@
require 'fixtures/topic'
require 'fixtures/reply'
require 'fixtures/task'
require 'fixtures/course'

class QueryCacheTest < Test::Unit::TestCase
fixtures :tasks
Expand Down Expand Up @@ -43,6 +44,16 @@ def test_cache_is_scoped_on_all_descending_classes
end
end

def test_cache_does_not_blow_up_other_connections
assert_not_equal Course.connection.object_id, Task.connection.object_id,
"Connections should be different, Course connects to a different database"

ActiveRecord::Base.cache do
assert_not_equal Course.connection.object_id, Task.connection.object_id,
"Connections should be different, Course connects to a different database"
end
end


end

Expand Down

0 comments on commit 7842cae

Please sign in to comment.