diff --git a/django/db/transaction.py b/django/db/transaction.py index ae009a40b48..921affc2d8b 100644 --- a/django/db/transaction.py +++ b/django/db/transaction.py @@ -18,7 +18,7 @@ from functools import wraps from django.db import ( connections, DEFAULT_DB_ALIAS, - DatabaseError, ProgrammingError) + DatabaseError, Error, ProgrammingError) from django.utils.decorators import available_attrs @@ -309,7 +309,12 @@ class Atomic(object): try: connection.commit() except DatabaseError: - connection.rollback() + try: + connection.rollback() + except Error: + # Error during rollback means the connection was + # closed. Clean up in case the server dropped it. + connection.close() raise else: # This flag will be set to True again if there isn't a savepoint @@ -330,7 +335,12 @@ class Atomic(object): connection.needs_rollback = True else: # Roll back transaction - connection.rollback() + try: + connection.rollback() + except Error: + # Error during rollback means the connection was + # closed. Clean up in case the server dropped it. + connection.close() finally: # Outermost block exit when autocommit was enabled. diff --git a/tests/backends/tests.py b/tests/backends/tests.py index 0da5cb85e20..e62f5648513 100644 --- a/tests/backends/tests.py +++ b/tests/backends/tests.py @@ -611,13 +611,6 @@ class BackendTestCase(TestCase): with self.assertRaises(DatabaseError): cursor.execute(query) - -class IsUsableTests(TransactionTestCase): - # Avoid using a regular TestCase because Django really dislikes closing - # the database connection inside a transaction at this point (#21202). - - available_apps = [] - # Unfortunately with sqlite3 the in-memory test database cannot be closed. @skipUnlessDBFeature('test_db_allows_multiple_connections') def test_is_usable_after_database_disconnects(self):