简体   繁体   English

是否保持Mysql连接处于活动状态或关闭它并再次打开?

[英]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.

相关问题 如何在NodeJS中保持恒定的MySQL连接存活 - How can I keep a constant MySQL connection alive in NodeJS 如何保持 PostgreSQL 连接打开? - How do I keep PostgreSQL connection open? 如何保持HTTP连接活着? - How to keep a HTTP connection alive? 如何强制 Google Doc 保存并关闭,然后再次打开以继续追加? (谷歌脚本) - How do I force a Google Doc to save and close, then open again to continue appending? (Google Script) Node.JS,服务器在请求中间关闭Keep-Alive连接 - Node.JS, Server close Keep-Alive connection in middle of request 如何在 js 中执行代码,当单击“打开”文本时,不仅会打开栏,还会将文本更改为“关闭”以再次关闭它? - How do I execute a code in js that when clicked on “open” text not only opens the bar but also changes the text to “close” to close it again? 在node.js中保持服务器客户端响应连接打开并保持活动状态是否不好? - Is it bad to keep a server client response connection open and alive in node.js? 如何使我的页面可以关闭或重新加载,但当我再次打开页面时内容不会消失? - How do I make it so my page can close or reload but the content wont disappear when i open the page again? POSTGRESQL - 从暴露端口的那一刻起保持数据库连接打开还是为每个请求打开和关闭连接更好? - POSTGRESQL - Is it better to keep a db connection open from the moment a port is exposed or to open and close a connection for each request? 使应用程序和NodeJS之间的连接保持活动2分钟 - Keep the connection between app and NodeJS alive for 2 minutes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM