﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
19519	Django fails to close db connections at end of request-response cycle	amosonn@…	Aymeric Augustin	"Under some circumstances (I'll get to guesses about this in the end), the db connection isn't closed at the end of the request, and resurfaces later on to service another request. Thus, one request writes to the database, and then the next request tries to look for that data and fails, because it uses an outdated connection that hasn't yet been notified of the db change (per [http://www.python.org/dev/peps/pep-0249/#connection-objects pep-249], different connections aren't required to see the same data.) The workaround solution for this is to add a django.db.connection.close() before that query, which brings to mind [https://code.djangoproject.com/ticket/13533 bug #13533]. Similarly, This bug manifests on mysql innodb; but I haven't checked other databases, so this might just be coincidence. It is also possible that the non-closing connection bug exists for all back-ends, but that auto-commit isn't enough to prevent the damage only on mysql innodb.

The situation under which I've seen these connections retained is when sending an HttpResponse with a generator for content. It might be that when sending such content, the following lines (django.db:44-47)
{{{
def close_connection(**kwargs):
    for conn in connections.all():
        conn.close()
signals.request_finished.connect(close_connection)
}}}
which are supposed to make sure the connection closes, don't work, because the signal isn't raised. It is, however, still a riddle how the old connection gets attached to a new request. [https://code.djangoproject.com/ticket/19117 Bug #19117] might also be related to this."	Bug	closed	Database layer (models, ORM)	dev	Release blocker	fixed	connection, managment, closing, mysql, innodb, signal, generator	Florian Apolloner	Ready for checkin	1	0	0	0	0	0
