在命令[django]中是否需要关闭数据库连接?

时间:2013-03-18 00:21:38

标签: django django-commands

根据此(http://djangosnippets.org/snippets/926/)代码段,连接在句柄中关闭。但这是一种旧代码。

在django 1.4中,我们必须关闭连接吗?我查看了django代码,但我找不到关闭连接的代码。

如果django关闭连接在哪里?

谢谢。

2 个答案:

答案 0 :(得分:1)

正如片段所述:

# Close the DB connection. This is required as a workaround for an
# edge case in MySQL: if the same connection is used to
# create tables, load data, and query, the query can return
# incorrect results. 

来自Django:

So, yes, if you do something to deliberately create lots of connections, 
lot of connections will be created. However, Django closes its connection to the 
database at the end of each request/response cycle, so there is only one connection 
in operation per thread or process handling requests and responses. If you're not 
using the HTTP layer, it's still only one connection per thread of execution and 
you are in complete control of the number of threads you create.

https://code.djangoproject.com/ticket/9878

答案 1 :(得分:0)

首先是免责声明:我不是专家(远非它)。

无论如何,您引用的代码段引用了两张票:Django票证表明关闭连接已包含在加载灯具的代码中(参见 django / core / management中的第55-60行) /commands/loaddata.py )。 MySQL票证表明他们身边没有任何改变。

无论如何,我认为这取决于你想要做什么。最重要的是,“连接关闭”修复似乎只是MySQL的一部分。如果您使用任何其他数据库,则不应出现代码段中注释所建议的错误。