summaryrefslogtreecommitdiff
path: root/django/db/backends/__init__.py
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2014-06-07 14:09:27 +0200
committerAymeric Augustin <aymeric.augustin@m4x.org>2014-06-07 14:37:43 +0200
commitcfcca7ccce3dc527d16757ff6dc978e50c4a2e61 (patch)
tree81b5ac70392c045d4ed7f9fa3adf7cd5d875cb96 /django/db/backends/__init__.py
parente2112edd9ac6a5f1877a62dd1c88d2d1726350de (diff)
Fixed #3711, #6734, #12581 -- Bounded connection.queries.
Prevented unlimited memory consumption when running background tasks with DEBUG=True. Thanks Rob, Alex, Baptiste, and others.
Diffstat (limited to 'django/db/backends/__init__.py')
-rw-r--r--django/db/backends/__init__.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
index bcc3979e2b..587cd874d9 100644
--- a/django/db/backends/__init__.py
+++ b/django/db/backends/__init__.py
@@ -1,3 +1,4 @@
+from collections import deque
import datetime
import time
import warnings
@@ -30,17 +31,21 @@ class BaseDatabaseWrapper(object):
ops = None
vendor = 'unknown'
+ queries_limit = 9000
+
def __init__(self, settings_dict, alias=DEFAULT_DB_ALIAS,
allow_thread_sharing=False):
# Connection related attributes.
+ # The underlying database connection.
self.connection = None
- self.queries = []
# `settings_dict` should be a dictionary containing keys such as
# NAME, USER, etc. It's called `settings_dict` instead of `settings`
# to disambiguate it from Django settings modules.
self.settings_dict = settings_dict
self.alias = alias
+ # Query logging in debug mode.
self.use_debug_cursor = None
+ self.queries_log = deque(maxlen=self.queries_limit)
# Transaction related attributes.
# Tracks if the connection is in autocommit mode. Per PEP 249, by
@@ -79,6 +84,14 @@ class BaseDatabaseWrapper(object):
def __hash__(self):
return hash(self.alias)
+ @property
+ def queries(self):
+ if len(self.queries_log) == self.queries_log.maxlen:
+ warnings.warn(
+ "Limit for query logging exceeded, only the last {} queries "
+ "will be returned.".format(self.queries_log.maxlen))
+ return list(self.queries_log)
+
##### Backend-specific methods for creating connections and cursors #####
def get_connection_params(self):
@@ -429,7 +442,7 @@ class BaseDatabaseWrapper(object):
def make_debug_cursor(self, cursor):
"""
- Creates a cursor that logs all queries in self.queries.
+ Creates a cursor that logs all queries in self.queries_log.
"""
return utils.CursorDebugWrapper(cursor, self)