1

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

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

如果 django 关闭连接,它在哪里?

谢谢你。

4

2 回答 2

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

于 2013-03-18T02:46:36.010 回答
0

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

无论如何,您引用的代码片段指的是两张票:Django票建议关闭连接已包含在用于加载固定装置的代码中(请参阅django/core/management/commands/loaddata.py中的第 55-60 行)。MySQL票表明他们方面没有太大变化。

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

于 2013-03-18T01:03:35.373 回答