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.
Fixes #12698.

View File

@ -273,7 +273,18 @@ module ActiveRecord
# Rolls back the transaction (and turns on auto-committing). Must be
# 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)
nil

View File

@ -3,7 +3,7 @@ module ActiveRecord
module QueryCache
class << self
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
def dirties_query_cache(base, *method_names)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -212,6 +212,38 @@ class QueryCacheTest < ActiveRecord::TestCase
ensure
ActiveRecord::Base.configurations = conf
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
class QueryCacheExpiryTest < ActiveRecord::TestCase