Merge pull request #17820 from fw42/restore_query_cache_on_rollback

Clear query cache on rollback
This commit is contained in:
Rafael Mendonça França 2015-01-02 14:34:25 -03:00
parent 7d8e56ab07
commit 5dc0bb9203
9 changed files with 53 additions and 9 deletions

View File

@ -1,3 +1,7 @@
* Clear query cache on rollback.
*Florian Weingarten*
* Fixed setting of foreign_key for through associations while building of new record. * Fixed setting of foreign_key for through associations while building of new record.
Fixes #12698. Fixes #12698.

View File

@ -273,7 +273,18 @@ module ActiveRecord
# Rolls back the transaction (and turns on auto-committing). Must be # Rolls back the transaction (and turns on auto-committing). Must be
# done if the transaction block raises an exception or returns false. # done if the transaction block raises an exception or returns false.
def rollback_db_transaction() end def rollback_db_transaction
exec_rollback_db_transaction
end
def exec_rollback_db_transaction() end #:nodoc:
def rollback_to_savepoint(name = nil)
exec_rollback_to_savepoint(name)
end
def exec_rollback_to_savepoint(name = nil) #:nodoc:
end
def default_sequence_name(table, column) def default_sequence_name(table, column)
nil nil

View File

@ -3,7 +3,7 @@ module ActiveRecord
module QueryCache module QueryCache
class << self class << self
def included(base) #:nodoc: def included(base) #:nodoc:
dirties_query_cache base, :insert, :update, :delete dirties_query_cache base, :insert, :update, :delete, :rollback_to_savepoint, :rollback_db_transaction
end end
def dirties_query_cache(base, *method_names) def dirties_query_cache(base, *method_names)

View File

@ -9,7 +9,7 @@ module ActiveRecord
execute("SAVEPOINT #{name}") execute("SAVEPOINT #{name}")
end end
def rollback_to_savepoint(name = current_savepoint_name) def exec_rollback_to_savepoint(name = current_savepoint_name)
execute("ROLLBACK TO SAVEPOINT #{name}") execute("ROLLBACK TO SAVEPOINT #{name}")
end end

View File

@ -330,9 +330,6 @@ module ActiveRecord
def create_savepoint(name = nil) def create_savepoint(name = nil)
end end
def rollback_to_savepoint(name = nil)
end
def release_savepoint(name = nil) def release_savepoint(name = nil)
end end

View File

@ -328,7 +328,7 @@ module ActiveRecord
execute "COMMIT" execute "COMMIT"
end end
def rollback_db_transaction #:nodoc: def exec_rollback_db_transaction #:nodoc:
execute "ROLLBACK" execute "ROLLBACK"
end end

View File

@ -208,7 +208,7 @@ module ActiveRecord
end end
# Aborts a transaction. # Aborts a transaction.
def rollback_db_transaction def exec_rollback_db_transaction
execute "ROLLBACK" execute "ROLLBACK"
end end
end end

View File

@ -362,7 +362,7 @@ module ActiveRecord
log('commit transaction',nil) { @connection.commit } log('commit transaction',nil) { @connection.commit }
end end
def rollback_db_transaction #:nodoc: def exec_rollback_db_transaction #:nodoc:
log('rollback transaction',nil) { @connection.rollback } log('rollback transaction',nil) { @connection.rollback }
end end

View File

@ -212,6 +212,38 @@ class QueryCacheTest < ActiveRecord::TestCase
ensure ensure
ActiveRecord::Base.configurations = conf ActiveRecord::Base.configurations = conf
end end
def test_query_cache_doesnt_leak_cached_results_of_rolled_back_queries
ActiveRecord::Base.connection.enable_query_cache!
post = Post.first
Post.transaction do
post.update_attributes(title: 'rollback')
assert_equal 1, Post.where(title: 'rollback').to_a.count
raise ActiveRecord::Rollback
end
assert_equal 0, Post.where(title: 'rollback').to_a.count
ActiveRecord::Base.connection.uncached do
assert_equal 0, Post.where(title: 'rollback').to_a.count
end
begin
Post.transaction do
post.update_attributes(title: 'rollback')
assert_equal 1, Post.where(title: 'rollback').to_a.count
raise 'broken'
end
rescue Exception
end
assert_equal 0, Post.where(title: 'rollback').to_a.count
ActiveRecord::Base.connection.uncached do
assert_equal 0, Post.where(title: 'rollback').to_a.count
end
end
end end
class QueryCacheExpiryTest < ActiveRecord::TestCase class QueryCacheExpiryTest < ActiveRecord::TestCase