不用持久化Mysql连接(Persistent Connection)的几种情况
发布于: May 19, 2010, 9:09 am 分类: PHP/MySQL 作者: Saturn 0 个评论
一直对PHP提供的持久化连接(Persistent Connection)的使用将信将疑,今天算是彻底做了个research,对其使用和应用场景清晰了很多,特此记录。
今天在CI论坛上有个帖子讨论PHP连接数据库时持久化链接的问题,我在这里做个总结 (就在写这篇文章的时候发现已经有人总结的很好了,于是直接转帖过来,没有必要重复劳动)。
以下内容来自PHP官方站点关于persistent connection的评论。
不使用持久化数据库连接的诸多理由和场景:
* When you lock a table, normally it is unlocked when the connection closes, but since persistent connections do not close, any tables you accidentally leave locked will remain locked, and the only way to unlock them is to wait for the connection to timeout or kill the process. The same locking problem occurs with transactions. (See comments below on 23-Apr-2002 & 12-Jul-2003)
* Normally temporary tables are dropped when the connection closes, but since persistent connections do not close, temporary tables aren't so temporary. If you do not explicitly drop temporary tables when you are done, that table will already exist for a new client reusing the same connection. The same problem occurs with setting session variables. (See comments below on 19-Nov-2004 & 07-Aug-2006)
* If PHP and MySQL are on the same server or local network, the connection time may be negligible, in which case there is no advantage to persistent connections.
* Apache does not work well with persistent connections. When it receives a request from a new client, instead of using one of the available children which already has a persistent connection open, it tends to spawn a new child, which must then open a new database connection. This causes excess processes which are just sleeping, wasting resources, and causing errors when you reach your maximum connections, plus it defeats any benefit of persistent connections. (See comments below on 03-Feb-2004, and the footnote at http://devzone.zend.com/node/view/id/686#fn1)
参考: