[英]Do I keep the Mysql connection alive or close it and open again?
I'm writing a code in PHP and JS for a project that will request data from a remote database every minute. 我正在用PHP和JS编写一个项目的代码,该项目将每分钟从远程数据库中请求数据。 The PHP makes the connection and return the data from my MYSQL database, and the JS is responsible for printing this data on my website and requesting it again.
PHP建立连接并从MYSQL数据库返回数据,而JS负责将这些数据打印在我的网站上并再次请求。 My doubt is: On the PHP file, do I keep the connection with the database server alive or close it and open it again every minute?
我的疑问是:在PHP文件上,我是否保持与数据库服务器的连接处于活动状态或关闭它,然后每分钟重新打开一次?
I'm sorry about my english. 我的英语不好意思。
Seems your application is a web page. 您的应用程序似乎是一个网页。 That means that the web server serving it will run a PHP process for each request and then it will die and so the connection will get closed with it.
这意味着为它提供服务的Web服务器将为每个请求运行一个PHP进程,然后它将终止,因此连接将被关闭。 Your question applies better to CLI applications specially if running in daemon mode.
您的问题尤其适用于在守护程序模式下运行的CLI应用程序。
UPDATE : as per @deceze comment " it's perfectly possible to have a persistent PHP script backing a web server… it's just not the default MO ". 更新 :根据@deceze注释“ 完全有可能有持久的PHP脚本支持Web服务器……这不是默认的MO ”。
close operation Releases the Connection object's database and resources immediately. close操作立即释放Connection对象的数据库和资源。 so if you have alot of computation on the open connection and extracted the full result from database etc, you may want to close the connection since this brings down the performance on the server.
因此,如果您在打开的连接上进行了大量计算并从数据库等中提取了完整的结果,则可能要关闭该连接,因为这会降低服务器的性能。 but if you don't have alot of computation on the connection you can keep it open until a trigger making it close(this trigger can be anything really, from a button like logging out to creating a timer for closing the connection).
但是如果您在连接上没有大量计算,则可以保持打开状态,直到触发器将其关闭为止(该触发器可以是任何东西,从注销之类的按钮到创建用于关闭连接的计时器)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.