关闭连接 [英] Closing connections

查看:63
本文介绍了关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在运行一个流行的网站,一年前没有任何问题。页面可能需要加载年龄,网站通常会关闭。虽然我们假设这是托管公司(并且正在移动主机),但我担心这可能是我在编程中所做的事情。特别地,我们已经具有多重连接。偶尔会出错。每个php页面连接到数据库,但我没有关闭连接,因为我知道这是自动发生的。那是对的吗?当我在测试网站时,我有时会在几个浏览器中打开它 - 这会导致问题吗?

感谢任何建议。

解决方案

< blockquote>只是一些可以帮助你的信息。


当您在浏览器中打开一个寻址PHP脚本的页面时,执行该脚本,发送它应该发送的任何内容回到用户的浏览器,然后脚本结束。然后,当脚本结束时,您与数据库的连接应该结束(尽管在这里我不能说使用pconnect,因为我在工作中没有使用过这个)。因此,即使您同时打开了许多浏览器,但这并不意味着您有许多与数据库打开的连接。


浏览器仍然打开显示页面的事实确实如此并不意味着后台中的脚本仍在运行,除非脚本中有某种循环导致它继续运行。但是之后脚本可能会相对快速地超时,除非您使用配置来允许脚本在超时之前运行很长时间。但是由于你使用的是外部主机,我不认为是这种情况。


需要考虑的一件事是你的数据库设计是否突然变得非常长去表演。随着表格变大,可能会发生这种情况。这通常可以通过添加一些表索引来修复,这可能会略微减慢更新并插入到表中,但可以极大地提高查询速度,例如100或更多因素。


< blockquote>嗨。


当你说热门时,我们是说每秒几次点击还是每秒几百或几千次点击?

如果它只是每秒几次点击,那么任何一半体面的服务器应该能够处理负载。通常只需要几分之一秒来呈现页面,默认情况下,Apache允许150个并发连接。


MySQL服务器可以处理的并发连接数也有限制。

如果我没记错的话,默认值为100.


如果你使用共享主机,那么同一台机器上的另一个站点是可能的造成这个问题。如果是这种情况你应该打电话给托管公司。


这样的减速的一个非常罕见的原因,因为一个小网站变得更加成熟,数据库设计,当有20行时效果很好在表中,但有200万时不太好。查看在慢速运行的页面上执行的查询并确保对正在执行查找的字段进行索引可能是值得的。


Hi
I''m running a popular site which hasn''t had any problems up to a year ago. Pages can take an age to load and the site is often down. Although we''re assuming this is down to the hosting company (and are in the process of moving host) I am concerned that it may be something I''ve done in the programming. In particular we have had a "multiple connection" error occasionally. Each php page connects to the database but I don''t close the connection as I understood that happened automatically. Is that correct? When I''m testing the site I sometimes open it in several browsers - could that cause a problem?
Any advice gratefully appreciated.

解决方案

Just some info that may help you out.

When you open a page in a browser that addresses a PHP script, that script is carried out, sends whatever it is supposed to send back to the user''s browser, and then the script ends. Your connection to the database should then end when the script ends (although here I cannot speak for the use of pconnect as I haven''t used this in my work). So even though you have many browsers open at one time, this does not necessarilly mean you have many connections to the database open.

The fact that the browser is still open showing a page does not mean that the script in the background is still running, unless you have some sort of loop in the script which causes it to continue. But then the script would probably time out relatively quickly unless you played around with the configurations to allow a script to run for a long time before timing out. But since you are using an external host, I would not suppose that this is the case.

One thing to consider is whether your database design is such that the queries have suddenly become extremely long to perform. This can happen as the tables become larger. And this can quite often be fixed by adding some table indexes, which may slow up slightly the update and inserts to tables, but can increase the speed of queries enormously, like factor of 100 or more.


Hi.

When you say "popular", are we talking a couple of hits per second or some hundreds or thousands of hits per second?

If it is only a few hits per second, then any half-decent server should be able to handle the load. It usually only takes a fraction of a second to render a page and by default Apache allows 150 concurrent connections.

There is also a limit to how many concurrent connections your MySQL server can handle.
If I remember correctly, the default value is 100.

It''s possible if you are using shared hosting that another site on the same machine is causing the problem. If that is the case you should call the hosting company.


A not-uncommon cause of slowdowns like this as a small site becomes more established is database design that works well when there are 20 rows in the table but not so well when there are 2 million. It may be worthwhile to look at what queries you''re doing on slow-running pages and make sure that fields you''re doing lookups on are indexed.


这篇关于关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆