显式cursor.close()的必要性 [英] Necessity of explicit cursor.close()

查看:3454
本文介绍了显式cursor.close()的必要性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会不时使用 c>

我注意到,在几个地方,我没有调用显式 cursor.close()后,我完成了数据库。到目前为止,这没有导致任何错误或性能问题。我想知道如果没有明确地关闭游标,我可能会遇到什么样的问题,什么可能出错?

I've noticed that in several places I don't call explicit cursor.close() after I'm done with database. So far, this hasn't result into any errors, or performance issues. I'm wondering what kind of problems could I possibly have without closing the cursor explicitly, what can go wrong?

据我所知, Django中的连接 cursor 遵循Python Database API Specification v2.0( PEP-249 )。根据它, cursor 会在每次调用 __ del __()方法时自动关闭。我想问题也可以是:有没有一个用例,当它不被调用?

As far as I understand, connection and cursor in Django follow "Python Database API Specification v2.0" (PEP-249). And, according to it, cursor would be automatically closed whenever __del__() method is called. I guess the question could be also: Is there a use case when it is not called?

FYI,我使用Python 2.7和Django 1.6.5。 p>

FYI, I'm using Python 2.7 and Django 1.6.5.

推荐答案

__ del __ / .close )

__del__/.close():


  1. __ del __ 不确保被调用

  2. 一些数据库不会在 __ del __ 中调用cursor.close()

  3. 一些数据库实际上并不在连接函数中创建连接,而是在游标函数中创建连接(例如对于2& 3:pyhive的presto [ / li>
  1. __del__ is not guaranteed to be called
  2. some databases don't call cursor.close() in their __del__ (bad practice, but true)
  3. some databases don't actually create connections in the connection function, but in the cursor function instead (e.g. for 2&3: pyhive's presto [maybe they've since patched it])

在服务器连接上

服务器有一个空闲超时配置属性(让我们称之为T)。如果连接空闲超过T秒,服务器将删除连接。大多数服务器还具有设置工作线程池(W)的大小的属性。如果您已经有W连接到您的服务器,它可能会在尝试新的连接时挂起。第二个想象是,您没有显式关闭连接的选项。在这种情况下,您必须将超时设置为足够小,以便您的工作池永远不会完全使用,这是您拥有多少并发连接的函数。

Most servers have an idle timeout configuration property (let's call that T). If a connection is idle for more than T seconds, the server will remove the connection. Most servers also have properties to set the size of the worker thread pool (W). If you already have W connections to your server, it will likely hang when a new connection is attempted. For a second imagine that you don't have the option to explicitly close connections. In that situation, you have to set the timeout to be small enough that your worker pool is never completely used, which is a function of how many concurrent connections you have.

但是,如果你关闭游标/连接(即使上面的[3]不等同,它们的行为方式类似),那么你不必管理这些服务器配置属性,你的线程池只需要很大足以管理所有并发连接(具有偶尔等待新资源的选项)。我看到一些服务器(例如Cassandra上的Titan)无法从线程池中的工作线程中恢复,因此整个服务器一直关闭,直到重新启动。

However, if you do close your cursors/connections (even when not equivalent by [3] above, they behave in a similar way), then you don't have to manage these server configuration properties and your thread pool simply needs to be large enough to manage all concurrent connections (with the option for an occasional wait for new resources). I've seen some servers (e.g. Titan on Cassandra) unable to recover from running out of workers in the thread pool, so the whole server goes down until restarted.

strong> TL / DR
如果您使用的开发良好的库,例如 dano 提及,就不会有问题。如果你使用较少的原始库,如果你不调用 .close(),根据你的服务器配置,你可能会在服务器上阻塞服务器获取一个工作者线程和访问率。

TL/DR If you're using very well-developed libraries, like the ones dano mentions, you won't have an issue. If you're using less pristine libraries, you may end up blocking on the server acquiring a worker thread if you don't call .close(), depending on your server config and access rates.

这篇关于显式cursor.close()的必要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆