﻿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
9878	Database backend leaves idling connections when used in multiple threads	Fredde	nobody	"When creating and saving model instances in multiple threads, the database backend leaves idling connections. This will finally cause a ""Too many connections""-exception.

I'm using Django 1.0 with a MySQL database

This script will leave ~10-20 open connections
{{{
def createArticle():
    a = Article(title = '...', text = '...')
    a.save()

threads = []
for i in xrange(200):
    t = threading.Thread(target = createArticle)
    t.start()
    threads.append(t)

for t in threads:
    t.join()
}}}

To see the active connections in the mysql client
{{{
mysql> show full processlist;
+--------+------+-----------+------+---------+------+-------+-----------------------+
| Id     | User | Host      | db   | Command | Time | State | Info                  |
+--------+------+-----------+------+---------+------+-------+-----------------------+
| 463987 | ---- | localhost | NULL | Query   |    0 | NULL  | show full processlist |
| 463994 | ---- | localhost | ---- | Sleep   |   44 |       | NULL                  |
| 464020 | ---- | localhost | ---- | Sleep   |   85 |       | NULL                  |
| 464042 | ---- | localhost | ---- | Sleep   |   85 |       | NULL                  |
| 464045 | ---- | localhost | ---- | Sleep   |   85 |       | NULL                  |
| 464048 | ---- | localhost | ---- | Sleep   |   85 |       | NULL                  |
| 464067 | ---- | localhost | ---- | Sleep   |   85 |       | NULL                  |
| 464093 | ---- | localhost | ---- | Sleep   |   85 |       | NULL                  |
| 464105 | ---- | localhost | ---- | Sleep   |   85 |       | NULL                  |
| 464136 | ---- | localhost | ---- | Sleep   |   85 |       | NULL                  |
| 464139 | ---- | localhost | ---- | Sleep   |   85 |       | NULL                  |
| 464165 | ---- | localhost | ---- | Sleep   |   85 |       | NULL                  |
| 464166 | ---- | localhost | ---- | Sleep   |   85 |       | NULL                  |
| 464183 | ---- | localhost | ---- | Sleep   |   85 |       | NULL                  |
+--------+------+-----------+------+---------+------+-------+-----------------------+
14 rows in set (0.00 sec)
}}}
"	Uncategorized	closed	Database layer (models, ORM)	1.0	Normal	invalid			Unreviewed	0	0	0	0	0	0
