1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Fixed #9964 -- Ensure that all database operations make transactions dirty, not just write operations. Many thanks to Shai Berger for his work and persistence on this issue.

This is BACKWARDS INCOMPATIBLE for anyone relying on the current behavior that allows manually managed read-only transactions to be left dangling without a manual commit or rollback.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15493 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee
2011-02-12 13:03:34 +00:00
parent d1cd53d9cf
commit 6314a1b42e
11 changed files with 271 additions and 47 deletions

View File

@@ -245,10 +245,11 @@ class BaseDatabaseWrapper(local):
self.connection = None
def cursor(self):
cursor = self._cursor()
if (self.use_debug_cursor or
(self.use_debug_cursor is None and settings.DEBUG)):
return self.make_debug_cursor(cursor)
cursor = self.make_debug_cursor(self._cursor())
else:
cursor = util.CursorWrapper(self._cursor(), self)
return cursor
def make_debug_cursor(self, cursor):

View File

@@ -5,12 +5,28 @@ from time import time
from django.utils.hashcompat import md5_constructor
from django.utils.log import getLogger
logger = getLogger('django.db.backends')
class CursorDebugWrapper(object):
def __init__(self, cursor, db):
class CursorWrapper(object):
def __init__(self, cursor, connection):
self.cursor = cursor
self.db = db # Instance of a BaseDatabaseWrapper subclass
self.connection = connection
def __getattr__(self, attr):
if self.connection.is_managed():
self.connection.set_dirty()
if attr in self.__dict__:
return self.__dict__[attr]
else:
return getattr(self.cursor, attr)
def __iter__(self):
return iter(self.cursor)
class CursorDebugWrapper(CursorWrapper):
def execute(self, sql, params=()):
start = time()
@@ -19,8 +35,8 @@ class CursorDebugWrapper(object):
finally:
stop = time()
duration = stop - start
sql = self.db.ops.last_executed_query(self.cursor, sql, params)
self.db.queries.append({
sql = self.connection.ops.last_executed_query(self.cursor, sql, params)
self.connection.queries.append({
'sql': sql,
'time': "%.3f" % duration,
})
@@ -35,7 +51,7 @@ class CursorDebugWrapper(object):
finally:
stop = time()
duration = stop - start
self.db.queries.append({
self.connection.queries.append({
'sql': '%s times: %s' % (len(param_list), sql),
'time': "%.3f" % duration,
})
@@ -52,6 +68,7 @@ class CursorDebugWrapper(object):
def __iter__(self):
return iter(self.cursor)
###############################################
# Converters from database (string) to Python #
###############################################