Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed some locations to work with autocommit=True

 - backends: supports_transactions()
 - select_for_update tests
  • Loading branch information...
commit da573fbb4172fb962c9f021fc0c03cf91b13e746 1 parent bd283aa
@akaariai akaariai authored
View
27 django/db/backends/__init__.py
@@ -417,15 +417,24 @@ def __init__(self, connection):
@cached_property
def supports_transactions(self):
"Confirm support for transactions"
- cursor = self.connection.cursor()
- cursor.execute('CREATE TABLE ROLLBACK_TEST (X INT)')
- self.connection._commit()
- cursor.execute('INSERT INTO ROLLBACK_TEST (X) VALUES (8)')
- self.connection._rollback()
- cursor.execute('SELECT COUNT(X) FROM ROLLBACK_TEST')
- count, = cursor.fetchone()
- cursor.execute('DROP TABLE ROLLBACK_TEST')
- self.connection._commit()
+ try:
+ # Make sure to run inside a managed transaction block,
+ # otherwise autocommit will cause the confimation to
+ # fail.
+ self.connection.enter_transaction_management()
+ self.connection.managed(True)
+ cursor = self.connection.cursor()
+ cursor.execute('CREATE TABLE ROLLBACK_TEST (X INT)')
+ self.connection._commit()
+ cursor.execute('INSERT INTO ROLLBACK_TEST (X) VALUES (8)')
+ self.connection._rollback()
+ cursor.execute('SELECT COUNT(X) FROM ROLLBACK_TEST')
+ count, = cursor.fetchone()
+ cursor.execute('DROP TABLE ROLLBACK_TEST')
+ self.connection._commit()
+ self.connection._dirty = False
+ finally:
+ self.connection.leave_transaction_management()
return count == 0
@cached_property
View
5 tests/modeltests/select_for_update/tests.py
@@ -36,6 +36,8 @@ def setUp(self):
# issuing a SELECT ... FOR UPDATE will block.
new_connections = ConnectionHandler(settings.DATABASES)
self.new_connection = new_connections[DEFAULT_DB_ALIAS]
+ self.new_connection.enter_transaction_management()
+ self.new_connection.managed(True)
# We need to set settings.DEBUG to True so we can capture
# the output SQL to examine.
@@ -48,6 +50,7 @@ def tearDown(self):
# this in the course of their run.
transaction.managed(False)
transaction.leave_transaction_management()
+ self.new_connection.leave_transaction_management()
except transaction.TransactionManagementError:
pass
self.new_connection.close()
@@ -66,7 +69,7 @@ def start_blocking_transaction(self):
'for_update': self.new_connection.ops.for_update_sql(),
}
self.cursor.execute(sql, ())
- result = self.cursor.fetchone()
+ self.cursor.fetchone()
def end_blocking_transaction(self):
# Roll back the blocking transaction.

0 comments on commit da573fb

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