Skip to content
Snippets Groups Projects
Commit e657a33e authored by Jared Hancock's avatar Jared Hancock Committed by Peter Rotich
Browse files

queryset: Fix circular reference error

This fixes an error where the ModelInstanceManager maintained a reference to
the QuerySet instance, and the QuerySet instance managed a reference to the
ModelInstanceManager instance (if it's the iterator for the query). Because
of the circular reference, if the iterator is not exhausted, then the
resource is not closed and the query remains open. This wastes memory and
prevents some other queries from running after such a situation happens.

This addresses the issue by removing the circular reference between the
QuerySet and the ModelInstanceManager.
parent f62e9669
Branches
Tags
No related merge requests found
...@@ -1606,15 +1606,22 @@ extends CachedResultSet { ...@@ -1606,15 +1606,22 @@ extends CachedResultSet {
class ModelInstanceManager class ModelInstanceManager
implements IteratorAggregate { implements IteratorAggregate {
var $queryset;
var $model; var $model;
var $map; var $map;
var $resource;
var $annnotations;
var $defer;
static $objectCache = array(); static $objectCache = array();
function __construct(QuerySet $queryset) { function __construct(QuerySet $queryset) {
$this->queryset = $queryset;
$this->model = $queryset->model; $this->model = $queryset->model;
$this->resource = $queryset->getQuery();
$cache = !$queryset->hasOption(QuerySet::OPT_NOCACHE);
$this->resource->setBuffered($cache);
$this->map = $this->resource->getMap();
$this->annotations = $queryset->annotations;
$this->defer = $queryset->defer;
} }
function cache($model) { function cache($model) {
...@@ -1669,7 +1676,7 @@ implements IteratorAggregate { ...@@ -1669,7 +1676,7 @@ implements IteratorAggregate {
return null; return null;
} }
} }
$annotations = $this->queryset->annotations; $annotations = $this->annotations;
$extras = array(); $extras = array();
// For annotations, drop them from the $fields list and add them to // For annotations, drop them from the $fields list and add them to
// an $extras list. The fields passed to the root model should only // an $extras list. The fields passed to the root model should only
...@@ -1688,7 +1695,7 @@ implements IteratorAggregate { ...@@ -1688,7 +1695,7 @@ implements IteratorAggregate {
// Construct and cache the object // Construct and cache the object
$m = $modelClass::$meta->newInstance($fields); $m = $modelClass::$meta->newInstance($fields);
// XXX: defer may refer to fields not in this model // XXX: defer may refer to fields not in this model
$m->__deferred__ = $this->queryset->defer; $m->__deferred__ = $this->defer;
$m->__onload(); $m->__onload();
if ($cache) if ($cache)
$this->cache($m); $this->cache($m);
...@@ -1758,10 +1765,6 @@ implements IteratorAggregate { ...@@ -1758,10 +1765,6 @@ implements IteratorAggregate {
} }
function getIterator() { function getIterator() {
$this->resource = $this->queryset->getQuery();
$this->map = $this->resource->getMap();
$cache = !$this->queryset->hasOption(QuerySet::OPT_NOCACHE);
$this->resource->setBuffered($cache);
$func = ($this->map) ? 'getRow' : 'getArray'; $func = ($this->map) ? 'getRow' : 'getArray';
$func = array($this->resource, $func); $func = array($this->resource, $func);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment