简体   繁体   English

在AJAX调用中使用mysql_pconnect时还要记住什么

[英]What else to keep in mind when using mysql_pconnect in AJAX calls

I build an application (MySQL/PHP) which (in time) may use connections to distant (and maybe slow) MySQL servers. 我构建了一个应用程序(MySQL / PHP),它(及时)可能会使用与远程(也可能很慢)MySQL服务器的连接。 The app uses AJAX whispers which open the DB connections frequently. 该应用程序使用AJAX耳语,经常打开数据库连接。 I have done some client side optimization, but never had courage enough to use persistent connections, which may be good idea here. 我已经做了一些客户端优化,但从来没有足够的勇气使用持久连接,这可能是个好主意。

I am worried about idle mysql connections which may exceed the server limit resulting in need to restart the server. 我担心空闲的mysql连接可能会超出服务器限制,导致需要重新启动服务器。 So I plan to use mysql.connection_timeout to 15s, which will kill every idle persistent connection after the limit? 所以我计划使用mysql.connection_timeout到15s,这将在限制后杀死每个空闲的持久连接? Or will it block system resources until the end of times of server process? 或者它会阻止系统资源,直到服务器进程结束?

I won't use transactions in whispers and every lock should be released after the connection is closed, right? 我不会在窃窃私语中使用交易,并且在连接关闭后应该释放每个锁,对吧?

Are there any other persistent connection issues I should be aware of? 我应该注意其他任何持续连接问题吗? (I have read the documentation already.) (我已经阅读了文档 。)

You don't generally gain anything by using persistent connections with MySQL. 通常不使用与MySQL的持久连接获得任何东西。 Its connection protocol is already generally fairly fast. 它的连接协议通常相当快。 What you do lose is the guarantee of a "clean" operating environment when you use regular non-persistent connections. 当您使用常规的非持久连接时,您所失去的是保证“干净”的操作环境。

If any of your scripts die for any reason while in the middle of a transaction or leave server-side variables set, a persistent connection means that half-done transaction is still active when the connection is picked up again by some OTHER script. 如果您的任何脚本在事务中间因任何原因而死亡或者设置了服务器端变量,则持久连接意味着当某些OTHER脚本再次获取连接时,半完成事务仍处于活动状态。

You can quite easily fall into a deadlock situation when this happens. 发生这种情况时,你很容易陷入僵局。 The new script has no idea that it's got an old/dirty connection, and MySQL cannot see that the original connecting script has gone away and do its own cleanup, so you end up with a garbage strewn environment. 新脚本不知道它有一个旧的/脏连接,并且MySQL无法看到原始的连接脚本已经消失并进行自己的清理,因此您最终得到了垃圾散布的环境。

That being said, you can limit how many connections PHP will keep open on your behalf with the mysql.max_persistent setting. 话虽这么说,您可以使用mysql.max_persistent设置限制PHP将代表您保持打开的连接数。 No matter how many connections you attemp to make from your scripts, PHP will never exceed that limit, so you can set it to be lower than MySQL's own max_connections setting. 无论您尝试从脚本中建立多少连接,PHP都不会超过该限制,因此您可以将其设置为低于MySQL自己的max_connections设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM