Skip to content

Commit 44164c5

Browse files
gsongaaugustin
authored andcommitted
Fixed django#19896 -- Committed after clearing cache in the database.
1 parent b6aede3 commit 44164c5

File tree

2 files changed

+9
-1
lines changed
  • django/core/cache/backends
  • tests/regressiontests/cache

2 files changed

+9
-1
lines changed

django/core/cache/backends/db.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ def clear(self):
184184
table = connections[db].ops.quote_name(self._table)
185185
cursor = connections[db].cursor()
186186
cursor.execute('DELETE FROM %s' % table)
187+
transaction.commit_unless_managed(using=db)
187188

188189
# For backwards compatibility
189190
class CacheClass(DatabaseCache):

tests/regressiontests/cache/tests.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from django.core.cache import get_cache
1919
from django.core.cache.backends.base import (CacheKeyWarning,
2020
InvalidCacheBackendError)
21-
from django.db import router
21+
from django.db import router, transaction
2222
from django.http import (HttpResponse, HttpRequest, StreamingHttpResponse,
2323
QueryDict)
2424
from django.middleware.cache import (FetchFromCacheMiddleware,
@@ -836,6 +836,13 @@ def test_second_call_doesnt_crash(self):
836836
interactive=False
837837
)
838838

839+
def test_clear_commits_transaction(self):
840+
# Ensure the database transaction is committed (#19896)
841+
self.cache.set("key1", "spam")
842+
self.cache.clear()
843+
transaction.rollback()
844+
self.assertEqual(self.cache.get("key1"), None)
845+
839846

840847
@override_settings(USE_TZ=True)
841848
class DBCacheWithTimeZoneTests(DBCacheTests):

0 commit comments

Comments
 (0)