mirror of https://github.com/django/django.git
[1.7.x] Fixed #21202 -- Maintained atomicity when the server disconnects.
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 81761508
from master
This commit is contained in:
parent
2e42c859da
commit
2ad0bc132a
|
@ -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
|
||||
from django.utils.deprecation import RemovedInDjango18Warning
|
||||
|
||||
|
@ -332,7 +332,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
|
||||
|
@ -353,7 +358,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.
|
||||
|
|
|
@ -663,13 +663,6 @@ class BackendTestCase(TestCase):
|
|||
self.assertTrue(isinstance(cursor, CursorWrapper))
|
||||
self.assertTrue(cursor.closed)
|
||||
|
||||
|
||||
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):
|
||||
|
|
Loading…
Reference in New Issue