在 PHP 中一遍又一遍地打开 MySQL 连接有什么影响

2022-01-24 00:00:00 connection php mysql

具体来说,我有一个 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:

er="nofollow norefer://www.google.com/webhp?hl=&sourceid=navclient-ff&rlz=1B3GGLL_enUS384US384&ie=UTF-8#rlz=1B3GGLL_enUS384US384&hl=en&source=hp&q=mysql+persistent+connection&;aq=0&aqi=g4g-m5&aql=&oq=mysql+persistent+conn&gs_rfai=Ch2c6iCchTO3zG4i6MZ-i7JAOAAAAqgQFT9BAKCs&fp=ff274912d96214e6

-- HTH

相关文章