Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.6.x] Fixed #21202 -- Maintained atomicity when the server disconne…

…cts.

Thanks intgr for the report.

This commit doesn't include a test because I don't know how to emulate a
database disconnection in a cross-database compatible way.

Also simplified a 'backends' test that was constrained by this problem.

Backport of 8176150 from master
  • Loading branch information...
commit 1d3d2b9a2458cbf11b7e828fb59b3f2cb73d14fe 1 parent 4ea02bd
@aaugustin aaugustin authored
Showing with 13 additions and 10 deletions.
  1. +13 −3 django/db/transaction.py
  2. +0 −7 tests/backends/tests.py
View
16 django/db/transaction.py
@@ -18,7 +18,7 @@
from django.db import (
connections, DEFAULT_DB_ALIAS,
- DatabaseError, ProgrammingError)
+ DatabaseError, Error, ProgrammingError)
from django.utils.decorators import available_attrs
@@ -309,7 +309,12 @@ def __exit__(self, exc_type, exc_value, traceback):
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 @@ def __exit__(self, exc_type, exc_value, traceback):
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.
View
7 tests/backends/tests.py
@@ -611,13 +611,6 @@ def test_duplicate_table_error(self):
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):
Please sign in to comment.
Something went wrong with that request. Please try again.