Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Removed try-except in django.db.close_connection()

The reason was that the except clause needed to remove a connection
from the django.db.connections dict, but other parts of Django do not
expect this to happen. In addition the except clause was silently
swallowing the exception messages.

Refs #19707, special thanks to Carl Meyer for pointing out that this
approach should be taken.
  • Loading branch information...
commit fafee74306e869c23edcef864f9d816565a5c4a2 1 parent c4841b3
Anssi Kääriäinen authored February 12, 2013
13  django/db/__init__.py
@@ -45,14 +45,11 @@ def close_connection(**kwargs):
45 45
     # Avoid circular imports
46 46
     from django.db import transaction
47 47
     for conn in connections:
48  
-        try:
49  
-            transaction.abort(conn)
50  
-            connections[conn].close()
51  
-        except Exception:
52  
-            # The connection's state is unknown, so it has to be
53  
-            # abandoned. This could happen for example if the network
54  
-            # connection has a failure.
55  
-            del connections[conn]
  48
+        # If an error happens here the connection will be left in broken
  49
+        # state. Once a good db connection is again available, the
  50
+        # connection state will be cleaned up.
  51
+        transaction.abort(conn)
  52
+        connections[conn].close()
56 53
 signals.request_finished.connect(close_connection)
57 54
 
58 55
 # Register an event that resets connection.queries
3  django/db/utils.py
@@ -99,9 +99,6 @@ def __getitem__(self, alias):
99 99
     def __setitem__(self, key, value):
100 100
         setattr(self._connections, key, value)
101 101
 
102  
-    def __delitem__(self, key):
103  
-        delattr(self._connections, key)
104  
-
105 102
     def __iter__(self):
106 103
         return iter(self.databases)
107 104
 
18  tests/regressiontests/requests/tests.py
@@ -548,9 +548,6 @@ def test_request_finished_db_state(self):
548 548
                      'This test will close the connection, in-memory '
549 549
                      'sqlite connections must not be closed.')
550 550
     def test_request_finished_failed_connection(self):
551  
-        # See comments in test_request_finished_db_state() for the self.client
552  
-        # usage.
553  
-        response = self.client.get('/')
554 551
         conn = connections[DEFAULT_DB_ALIAS]
555 552
         conn.enter_transaction_management()
556 553
         conn.managed(True)
@@ -560,9 +557,14 @@ def test_request_finished_failed_connection(self):
560 557
         def fail_horribly():
561 558
             raise Exception("Horrible failure!")
562 559
         conn._rollback = fail_horribly
563  
-        signals.request_finished.send(sender=response._handler_class)
564  
-        # As even rollback wasn't possible the connection wrapper itself was
565  
-        # abandoned. Accessing the connections[alias] will create a new
566  
-        # connection wrapper, whch must be different than the original one.
567  
-        self.assertIsNot(conn, connections[DEFAULT_DB_ALIAS])
  560
+        try:
  561
+            with self.assertRaises(Exception):
  562
+                signals.request_finished.send(sender=self.__class__)
  563
+            # The connection's state wasn't cleaned up
  564
+            self.assertTrue(len(connection.transaction_state), 1)
  565
+        finally:
  566
+            del conn._rollback
  567
+        # The connection will be cleaned on next request where the conn
  568
+        # works again.
  569
+        signals.request_finished.send(sender=self.__class__)
568 570
         self.assertEqual(len(connection.transaction_state), 0)

0 notes on commit fafee74

Please sign in to comment.
Something went wrong with that request. Please try again.