David Winterbottom committed 8766f3d

CommittingTransactionMiddleware now creates transaction for every database in DATABASES settings.

Without this, queries going to a database other than "default" were not being
committed. This was because Django's transaction middleware only opens a transaction
for the "default" database. Hence no commits are issued for the other databases
for johnny-cache to pick up on.

This fix simply iterates over the databases in the DATABASES setting and opens a
committing transaction for each.

  • Participants
  • Parent commits 08b67e1

Comments (0)

Files changed (1)

File johnny/

 import django
 from django.core.exceptions import ImproperlyConfigured
+from django.conf import settings
 from django.core.cache import get_cache
 from django.middleware import transaction as trans_middleware
 from django.db import transaction
         return resp
 class CommittingTransactionMiddleware(trans_middleware.TransactionMiddleware):
     """A version of the built in TransactionMiddleware that always commits its
     transactions, even if they aren't dirty."""
+    def process_request(self, request):
+        for alias in DATABASE_ALIASES:
+            transaction.enter_transaction_management(using=alias)
+            transaction.managed(True, using=alias)
+    def process_exception(self, request, exception):
+        for alias in DATABASE_ALIASES:
+            if transaction.is_dirty(using=alias):
+                transaction.rollback(using=alias)
+            transaction.leave_transaction_management(using=alias)
     def process_response(self, request, response):
-        if transaction.is_managed():
-            try: transaction.commit()
-            except: pass
-            transaction.leave_transaction_management()
+        for alias in DATABASE_ALIASES:
+            if transaction.is_managed(using=alias):
+                try:
+                    transaction.commit(using=alias)
+                except:
+                    # We ignore any errors
+                    pass
+                transaction.leave_transaction_management(using=alias)
         return response