在PHP中反复打开MySQL连接的影响是什么? [英] What are the implications of opening MySQL connections over and over again in PHP
问题描述
具体来说,我有一个DB类,每次调用类中的Query函数时,都会打开和关闭多个MySQL连接。我应该打开一次连接吗?
Specifically, I have a DB class that opens and closes multiple MySQL connections every time I call the Query function in the class. Should I open a connection once? or is it ok to open and close connections like this?
推荐答案
我简单的想法(ISAM,没有交易)C语言应用程序每天运行8小时,通过一个单一的MySQL连接在一个数据库中更新多个表,并保持打开整个时间。它工作正常。任何时候有任何类型的MySQL错误(不仅仅是服务器消失了),代码只是再次调用mysql_real_connect(),它选择没有任何麻烦。重新连接是我认为MySQL功能完美无缺的地方之一。
My simple-minded (ISAM, no transactions) C-language app runs for eight hours a day, updating multiple tables in one database over one single MySQL connection that stays open the whole time. It works just fine. Anytime there's any kind of MySQL error (not only server gone away), the code just calls mysql_real_connect() again and it picks right up without any trouble. Reconnection is one of the places where, in my opinion, MySQL functions flawlessly.
但是有很多争论和讨论关于持续连接的好/坏。您可以在这里找到一些:
But there's plenty of controversy and discussion about the goodness/badness of persistent connections. You can find some of it here:
- HTH
这篇关于在PHP中反复打开MySQL连接的影响是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!